Get current user from View

So, what I want to do, in my main layout there is a menu for user login and another for anonymous users.

The layout will be used on every page, so I'm not sure how to do it, as I saw, the Auth component can only be used in the controller, it would be nice if I had to do this in only one view, but for each view, how I can do it? Do I have to do something on the AppController?

What I want to do is basically

// layout <?php if(logged): ?> Welcome <?php echo $user; ?> <?php else: ?> Welcom anon, Log in? <?php endif; ?> 
+6
source share
1 answer

You can access the registered user in your view using the Auth component. From the manual:

Once a user logs in, you often need information about the current user. You can access the registered user using AuthComponent :: user (). This method is static and can be used globally after loading AuthComponent. You can use it as an instance method or as a static method:

 // Use anywhere AuthComponent::user('id') // From inside a controller $this->Auth->user('id'); 

You should be able to do something like:

 // layout <?php if(AuthComponent::user('name')): ?> Welcome <?php echo AuthComponent::user('name'); ?> <?php else: ?> Welcom anon, Log in? <?php endif; ?> 
+15
source

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


All Articles