More Views

We can now add and list our users, but we still haven't completed the functions and views to allow logging out, editing, and viewing our users.

Add the Logout function

If a user wants to logout, we need to add a function to enable this. After logging out, the user is redirected, so no view is required. We previously identified logout() as an unauthenticated action because we don't want logged out users to be automatically redirected to /user/logout after they log in. Add the following function to /src/Controller/UsersController.php:

  public function logout()
  {
    $result = $this->Authentication->getResult();
    if ($result && $result->isValid()) {
      $this->Authentication->logout();
      $this->Flash->success('You are now logged out.');
      return $this->redirect(['controller' => 'Users', 'action' => 'login']);
  	}
    return $this->redirect(['action' => 'index']);
  }

If a user is logged in ($result is valid) they are logged out and redirected to /users/login, otherwise the user is redirected to /users/index.

Create View and Edit Views

We already created our view() and edit() functions during Controller and Views, so now we need to create the Views for our functions so we can view and edit our users.

Create /templates/Users/view.php

<?php $this->assign('title', $user->full_name); ?>
<div class="row">
  <aside class="column">
    <div class="side-nav">
      <h4 class="heading"><?php echo __('Actions') ?></h4>
      <?php echo $this->Html->link(__('Edit User'), ['action' => 'edit', $user->id], ['class' => 'side-nav-item']) ?>
      <?php echo $this->Html->link(__('List Users'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
    </div>
  </aside>
  <div class="column column-80">
    <div class="users view content">
      <h3><?php echo h($user->full_name) ?></h3>
      <table>
        <tr>
          <th><?php echo __('Username') ?></th>
          <td><?php echo h($user->username) ?></td>
        </tr>
        <tr>
          <th><?php echo __('First Name') ?></th>
          <td><?php echo h($user->first_name) ?></td>
        </tr>
        <tr>
          <th><?php echo __('Last Name') ?></th>
          <td><?php echo h($user->last_name) ?></td>
        </tr>
        <tr>
          <th><?php echo __('Email') ?></th>
          <td><?php echo $this->Text->autoLinkEmails($user->email) ?></td>
        </tr>
        <tr>
          <th><?php echo __('Role') ?></th>
          <td><?php echo h($user->role) ?></td>
        </tr>
        <tr>
          <th><?php echo __('Modified') ?></th>
          <td><?php echo h($user->modified) ?></td>
        </tr>
        <tr>
          <th><?php echo __('Created') ?></th>
          <td><?php echo h($user->created) ?></td>
        </tr>
      </table>
    </div>
  </div>
</div>

On the user's view page you'll see the new date formats we implemented in Time Localization.

Create /templates/Users/edit.php

<?php $this->assign('title', 'Edit User'); ?>
<div class="row">
  <aside class="column">
    <div class="side-nav">
      <h4 class="heading"><?php echo __('Actions') ?></h4>
      <?= $this->Form->postLink(
                __('Delete'),
                ['action' => 'delete', $user->id],
                ['confirm' => __('Are you sure you want to delete # {0} ({1})?', $user->name, $user->id), 'class' => 'side-nav-item']
            ) ?>
      <?php echo $this->Html->link(__('List Users'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
    </div>
  </aside>
  <div class="column column-80">
    <div class="users form content">
      <?php echo $this->Form->create($user) ?>
      <fieldset>
        <legend><?php echo __('Edit User') ?></legend>
        <?php
          echo $this->Form->control('username', ['autofocus' => true]);
          echo $this->Form->control('first_name');
          echo $this->Form->control('last_name');
          echo $this->Form->control('password', ['value' => '']);
        ?>
        <p class="helper">Passwords must be at least 8 characters and contain at least 1 number, 1 uppercase, 1 lowercase, and 1 special character</p>
        <?php
          echo $this->Form->control('confirm_password', ['type' => 'password', 'value' => '']);
          echo $this->Form->control('email');
        ?>
      </fieldset>
      <?php echo $this->Form->button(__('Submit')) ?>
      <?php echo $this->Html->link(__('Cancel'), ['action' => 'view', $user->slug], ['class' => 'button']); ?>
      <?php echo $this->Form->end() ?>
      <?php echo $this->Form->postLink(__('Delete User'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete {0}?', $user->full_name), 'class' => 'side-nav-item']) ?>
    </div>
  </div>
</div>

Phone Number Views

Practice creating the Phone Number view and edit views.