php - Call to undefined method AuthenticationControllerComponentAuthenticationComponent::user()
one text
Solution:
You were probably looking at the wrong docs, specifically at the docs of the deprecated Auth
component that belongs to the CakePHP core.
You should refer to the docs of the new authentication plugin, which is where its Authentication
component usage is documented. The method for accessing the user via the Authentication
component is called getIdentity
:
$identity = $this->Authentication->getIdentity();
Furthermore, for your specific use case there is also a view helper that provides a method for checking whether the user is logged in. In your AppView::initialize()
method (src/View/AppView.php
) load the helper:
$this->loadHelper('Authentication.Identity');
Then you can use its isLoggedIn
method in your view templates, no further code needed in your controller:
<?php if($this->Identity->isLoggedIn()): ?>
...
<?php endif; ?>
See also
- Authentication Cookbook > Authentication Component > Accessing the logged in user
- Authentication Cookbook > View Helper