I’m using CakePHP 2.0 and I’m trying to understand how I can echo dynamic contents if the user is logged in the application.
In the view I would like to echo a menu to login or logout an user, how can I do that?
// I'm in the default template view
if (!AuthComponent::loggedIn()) {
$menu = $this->Html->link('Login', array('controller' => 'users', 'action' => 'login'));
$menu .= $this->Html->link('Register', array('controller' => 'users', 'action' => 'register'));
} else {
$menu = $this->Html->link('Home', array('controller' => 'users', 'action' => AuthComponent::user('id'), AuthComponent::user('username')));
$menu .= $this->Html->link('Logout', array('controller' => 'users', 'action' => 'logout'));
}
echo $menu;
I thought something like this but I’ve read It breaks the MVC rules.
How should I do things like that in CakePHP?
Does exists some example online?
You could set if they’re logged in or not in a controller then use that element accordingly.
In your controller:
In your layout:
Then have a memberBar element and a guestBar element:
You could pass the AuthComponent data to the element to avoid using the object in your layout.