CakePHP 2 - check if user is registered

I need a display element depending on whether the user is registered or not - In CakePHP 2.0

This does not work

<?php if ($this->Auth->loggedIn() { echo $this->element('user'); } else { echo $this->element('guest'); } ?> 

thanks

+4
source share
4 answers

Follow the MVC pattern and put the logic in the controller.

In the controller:

 $this->set( 'loggedIn', $this->Auth->loggedIn() ); 

In view:

 if( $loggedIn ) { echo $this->element( 'user' ); } else { echo $this->element( 'guest' ); } 
+14
source

Use the session assistant (required for authentication, as shown in the "log tutorial"):

 if ($this->Session->read('Auth.User')) { echo 'logged'; } else { echo 'guest'; } 
+4
source

Try the following:

 $element = (AuthComponent::loggedIn()) ? 'user' : 'guest'; echo $this->element($element); 

Pretty similar to what you have already tried, but then statically set the loggedIn method.

+2
source
 !$this->Session->check('marketplace_showlink' 

if in the above code an error occurs that is encoded in appcontroller

as:
Calling a member function of a function () for a non-object

it will be generated due to an empty session variable

+1
source

Source: https://habr.com/ru/post/1397317/


All Articles