Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cake php and using auth in layout

I am using auth component and it works ok. But in my default layout before the content I have some menu which is different if user is logged in. So I want to determine if user is logged in or not - normally I use $this->Auth->user('id') but $this->Auth doesnt work in layout (it only works in view which controller is using Auth component).

How to do it?

like image 261
user606521 Avatar asked Dec 01 '22 22:12

user606521


1 Answers

In beforeRender() just call

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

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

In order to get data in layout, you should call beforeRender() method in AppController.

Passing it through session is not a good idea IMHO. It might be not the usual case but at least I prefer to do things solid: If you're using the session for that your code will fail in a system that is not using a session (stateless auth). Overall I'm not a big fan of accessing the session in a view at all. Session is for me more like a datasource.

like image 153
floriank Avatar answered Dec 29 '22 18:12

floriank