In CakePHP 2.x you could do
AuthComponent::user()
in View to get data from Auth component. In CakePHP 3.0beta3 it throws:
"Error: Class 'AuthComponent' not found"
Is there a simple way to get data from AuthComponent in View?
In View:
$this->request->session()->read('Auth.User.username');
In Controller
$this->Auth->user('username');
You should have never used the AuthComponent in views in the first place.
It is better to either pass down the data from the controller to the view and access that, or better yet, use a AuthHelper to wrapper-access it easily (by reading from the session there for example).
An example would be AuthUser (
https://github.com/dereuromark/cakephp-tools/blob/master/src/View/Helper/AuthUserHelper.php ):
$this->AuthUser->id();
$this->AuthUser->user('username');
etc
The helper way doesn't require additional use statements in your view ctps and keeps them lean.
It also prevents notices when trying to access undefined indexed automatically.
if ($this->AuthUser->user('foobarbaz')) { // no error thrown even if it never existed
}
Cake 3.5
In AppController:
public function beforeRender(Event $event) {
....
$this->set('Auth', $this->components()->__get('Auth'));
}
In .ctp template:
<?php if (!$Auth->user()) { ?>
<a class="login" href="<?php echo $this->Url->build($Auth->getConfig('loginAction')); ?>">Login</a>
<?php } else { ?>
<div class="name"><?php echo h($Auth->user('name')); ?></div>
<?php } ?>
The AuthComponent is deprecated as of 4.0.0 and will be replaced by the authorization and authentication plugins.
Authentication plugin has built in View Helper.
Related
I understand that we can cache an action or view page in CakePHP 2.0. Refer to this link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html.
class PostsController extends AppController {
public $helpers = array('Cache');
}
public $cacheAction = array(
'view' => 36000,
'index' => 48000
);
However, it seems that CakePHP 3.0 has removed helper(http://book.cakephp.org/3.0/en/core-libraries/caching.html). Is there any other way in CakePHP 3.0 I can cache the view page. For example, I have a index view/action. And I would like to cache that page. Thanks.
Is there any other way in CakePHP 3.0 I can cache the view page.
Not without developing something yourself. Like for example saving the rendered content to a file in callback for Dispatcher.afterDispatch event and then checking for the file in callback for Dispatcher.beforeDispatch event and returning the cached response.
Better to use you something like Varnish which is more suited for the job.
I have admin authentication with the following beforeFilter() method in appController.php.
I am using cakePhp 2.4
public function beforeFilter() {
$this->Auth->allow(
array(
'controller'=>
'Services','Projects','News','Jobs','Messages',
'action'=>
'index','view'
)
);
}
I want to allow access to
1- index action of all controllers
2- view action of only Jobs controller
currently I have allowed access to index and view actions of all controllers.
how to fix?
Use $this-> Auth-> allow(array('index')) in appcontroller and$this-> Auth-> allow(array('index', 'view')) in JobsController.
It’s a common problem to CakePHP developer to auth allow to specific actions of a specific controller. See this article to solve it.
https://blog.sohelrana.me/cakephp-auth-allow-specific-actions-specific-controllers/
I'm using CakePHP 2.3. and I'm trying to cache my home page view. But After caching
it to home.php, I can't load cached view because of error:
Error: Class 'AuthComponent' not found
File: C:\wamp\www\project\trunk\app\tmp\cache\views\home.php
Line: 87
I use AuthComponent for realizing if user is logged in or not. Without caching Everything works.
Controller code:
public $helpers = array('Cache');
public $cacheAction = array(
'home' => '60 minutes',
);
Thanks
App::uses() your Auth component in your bootstrap would help:
App::uses('AuthComponent', 'Controller/Component');
This way Cake knows where to load the class from if its needed (even in cache mode).
That would be this line in the file \config\bootstrap.php
App::uses('AuthComponent', 'Controller/Component');
Note that I tried this in Cake 4.1.5 but it tlls me "Class 'AuthComponent' not found"
I'm trying to get the Mailchimp datasource from Springest working, and I'm having issues. I had to modify some code to get rid of CakePHP errors. Now it happens without errors, but there is no actual API request made to Mailchimp.
The Model, Datasource and Controller function code is exactly the same as in the download from Springest.
The code in my database.php file is:
public $mailchimp = array(
'datasource' => 'MailchimpSubscriber',
'apiKey' => '<my mailchimp API key>',
'defaultListId' => '<my list identifier>',
'baseUrl' => 'http://us1.api.mailchimp.com/1.2/'
);
I am running CakePHP 2.1.0 on XAMPP on my Macbook Pro.
Anybody got any clues?
==UPDATE==
I've uploaded a stripped-out controller here: https://gist.github.com/3011716
Here is the code for my view:
<h2>Subscribe to Mailchimp</h2>
<?php
echo $this->Form->create('People', array('action' => 'subscribe'));
echo $this->Form->input('id');
echo $this->Form->input('emailaddress');
echo $this->Form->input('FNAME');
echo $this->Form->input('LNAME');
echo $this->Form->input('GENDER');
echo $this->Form->end('Submit');
?>
Thanks for trying out our Mailchimp Datasource. One of the problems might be that is was written for CakePHP 1.2+ and that a lot of stuff has changed in CakePHP since the 2.0 release.
Note that we're still using Cake 1.3 with the datasource and experience no errors whatsoever.
Did you check out the migration guide from CakePHP 1.3 to 2.0? Right at the bottom there's some mentioning of changes done to DataSources, so the error might be related to that.
We'll try to take it for a spin in Cake 2.0 and see what goes wrong, but in the mean time, this might be helpful.
==UPDATE==
Yup, it was CakePHP 2.0 that was not compatible. I've ported the datasource to support CakePHP 2.0, please check the updated repository.
== SECOND UPDATE==
You've made one error in your view: because you've created the form with $this->Form->create('People', array('action' => 'subscribe')); the data of the form will be in the $this->data array with key People whereas it should have the key MailchimpSubscriber. Because there's no key with that name in your $this->data the MailchimpSubscriber model will ignore the save action. You'll need to change your submission form in such a way that the submitted data has the right model name as key.
Another note: you're now loading the MailchimpSubscriber model twice. The first time in the $uses array in the controller and the second time through the $this->loadModel() method in the controller action. I would only use the latter.
** SOLUTION **
I am using cakephp 1.3. I did not have to add any routes. All i did is after adding the action to the controller, I went to the url
/controller/action/id
in my case it was
/test/bravo/123
and that did the trick
** End **
I am using cakephp 1.3 and I am trying to add webservice. I heard its very easy to add restful. What I want to do is to have one of the actions in my controller provoked and log into a logfile as a success. Lets say
function bravo(){ $this->log("success", "Testlog");
So far I went through documentation and added the following to my routes.php
Router::mapResources('tests');
Router::parseExtensions();
then in test controller I have this
<?php
class TestsController extends AppController {
var $name = 'Tests';
var $components = array('RequestHandler');
.....
.....
function bravo(){
$this->log("success", "Testlog");
}
?>
Now when I go to
/tests.bravo
, its not logging the success in the log file. What am I doing wrong. Thanks