Elements are reusable blocks of code you can create once and use in multiple Views of your site, making it easier to make changes in a single location instead of searching for all the occurrences of the same code.
For ease of access I create a common login/logout block to display on all the pages of my applications.
Create /templates/element/admin.php
<div class="content">
<?php
$identity = $this->getRequest()->getAttribute('identity');
if (empty($identity)) {
echo $this->Html->link(__('Login'), ['controller' => 'users', 'action' => 'login']);
} else {
echo 'Welcome ' . $this->Html->link($identity->get('full_name'), ['controller' => 'Users', 'action' => 'view', $identity->get('slug')]) . ' : ' . $this->Html->link(__('logout'), ['controller' => 'users', 'action' => 'logout']);
}
?></div>
With the element created, you can then add the following code to the first nav
of the default layout in /templates/layout/default.php
to display the element on all pages except /users/login
.
...
<nav class="top-nav">
<?php
// If not the login view
if ($this->request->getParam('action') !== 'login') {
echo $this->element('admin');
} ?>
...
Now browse to your site at http://localhost/tutorial/users and observe the block. If you don't like where it is located, move the block where you want to display it in the layout page and you should see it displayed.