CakePHP 3: how to check if a user is registered

In CakePHP 3, I found two ways to find if a user is registered.

1st decision

if(!is_null($this->Auth->user('id'))){ // Logged in } 

2nd decision

 if (!is_null($this->request->session()->read('Auth.User.id'))) { // Logged in } 

I think the first one is better because it is short and short.

Is there a better way to check if a user is registered?

I'm not looking for speed necessarily. I want a clean and expressive way to write it.

+5
source share
2 answers

I think the best way:

 if ($this->Auth->user()) {...} 
+11
source

You can do this using the session() helper.

 $loggeduser = $this->request->session()->read('Auth.User'); if(!$loggeduser) { $userID = $loggeduser['id']; $firstName = $loggeduser['first_name']; } 
+1
source

All Articles