Cake php and using auth in layout

I am using the auth component and it works fine. But in my default layout, before the content, I have a menu that is different from when the user was logged in. Therefore, I want to determine whether the user is registered or not - I usually use $ this-> Auth-> user ('id'), but $ this-> Auth does not work in the layout (it only works based on which controller uses the component Auth).

How to do it?

+4
source share
2 answers

You can read Auth data from a session. Sort of:

$user = $session->read('Auth'); 

Remember to add the session assistant to your AppController.

 var $helpers = array('Session'); 
+6
source

In beforeRender () just call

 $this->set('userData', $this->Auth->user()); 

and set the data in the view and do your checks in the view.

To get the data in the layout, you must call the beforeRender() method in the AppController.

Passing this through the session is not a good idea IMHO. This may not be the usual case, but at least I prefer to do something solid: if you use a session for this, your code will fail on a system that does not use a session (autat). In general, I am not a big fan of access to the session as a whole. Session for me is more like a data source.

+12
source

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


All Articles