Thursday, November 4, 2010

View: User Index

Views control the display of your application. When the controller renders views, Cake will automatically look for a file name the same as the controller action. The index method is the default method called when the controller is called without an explicit action defined, so index.ctp is the default view.

Create /app/views/users/index.ctp

This file contains any actual display code, such as html, that will be used to view the results of the index() method.
<div class="users index">
    <table cellpadding="0" cellspacing="0">
    <tr>
        <th>ID</th>
        <th>Username</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th class="actions">Actions</th>
    </tr>
    <?php
    $i = 0;
    foreach ($users as $user):
        $class = null;
        if ($i++ % 2 == 0) {
            $class = ' class="altrow"';
        }
    ?>
    <tr<?php echo $class;?>>
        <td><?php echo $user['User']['id'];?></td>
        <td><?php echo $this->Html->link($user['User']['username'], array('controller' => 'users', 'action' => 'view', $user['User']['id'])); ?></td>
        <td>%lt;?php echo $user['User']['first_name']; ?></td>
        <td>%lt;?php echo $user['User']['last_name']; ?></td>
        <td>%lt;?php echo $user['User']['email']; ?></td>
        <td class="actions">
            <?php echo $this->Html->link(__('View', true), array('controller' => 'users', 'action' => 'view', $user['User']['id'])); ?>
            <?php echo $this->Html->link(__('Edit', true), array('controller' => 'users', 'action' => 'edit', $user['User']['id'])); ?>
            <?php echo $this->Html->link(__('Delete',true), array('action' => 'delete', $user['User']['id']), null, sprintf('Are you sure you want to delete # %s?', $user['User']['id'])); ?>
        </td>
    </tr>
<?php endforeach; ?>
    </table>
</div>
<div class="actions">
    <h3><?php __('Actions'); ?></h3>
    <ul>
        <li><?php echo $this->Html->link(__('New User', true), array('action' => 'add')); ?></li>
        <li><?php echo $this->Html->link(__('List Carriers', true), array('controller' => 'carriers')); ?></li>
        <li><?php echo $this->Html->link(__('New Carrier', true), array('controller' => 'carriers', 'action' => 'add')); ?></li>
        <li><?php echo $this->Html->link(__('List Reminders', true), array('controller' => 'reminders')); ?></li>
        <li><?php echo $this->Html->link(__('New Reminder', true), array('controller' => 'reminders', 'action' => 'add')); ?></li>
    </ul>
</div>
Edit /app/controller/users_controller.php Add the following method
    function index() {
        $this->set('users',$this->User->find('all'));
    }
As mentioned previously, any defined methods will override scaffolding so browse to the site root/users and view the results.

Browse to http://drug-ed.com/users/ to test the page. Observe your default user then click on the link for viewing and editing. Notice how the scaffolding still works for all methods other than those defined.

The double underscore (__) is a global CakePHP method that handles localization. Feel free to ignore it at this point.

No comments:

Post a Comment