Cakephp login and registration example

Cakephp login and registration example

First is first, go to CakePHP website and download the latest version of it. Extract it, inside the your htdocs folder and rename as “logreg”. Now open this folder with your IDE and start coding.

Step1:

Let’s create our database. We will have two tables called posts and users. Post table contains all of your posts and the user table contains your user information including your encrypted password.

1
--
2
-- Database: `logregcake`
3
--
4

5
CREATE DATABASE `logregcake`;
6

7
USE `logregcake`;
8

9

10
-- --------------------------------------------------------
11

12
--
13
-- Table structure for table `posts`
14
--
15

16
CREATE TABLE IF NOT EXISTS `posts` (
17
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
18
  `title` varchar(50) DEFAULT NULL,
19
  `body` text,
20
  `created` datetime DEFAULT NULL,
21
  `modified` datetime DEFAULT NULL,
22
  PRIMARY KEY (`id`)
23
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
24

25

26
-- --------------------------------------------------------
27

28
--
29
-- Table structure for table `users`
30
--
31

32
CREATE TABLE IF NOT EXISTS `users` (
33
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
34
  `username` varchar(50) DEFAULT NULL,
35
  `password` varchar(50) DEFAULT NULL,
36
  `email` varchar(50) NOT NULL,
37
  `phone` varchar(50) DEFAULT NULL,
38
  `role` varchar(20) DEFAULT NULL,
39
  `created` datetime DEFAULT NULL,
40
  `modified` datetime DEFAULT NULL,
41
  PRIMARY KEY (`id`)
42
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Step 2: Rename database.php.default to database.php from config folder and give your database credentials,


1
public $default = array(
2
        'datasource' => 'Database/Mysql',
3
        'persistent' => false,
4
        'host' => 'localhost',
5
        'login' => 'root',
6
        'password' => '',
7
        'database' => 'logregcake',
8
        'prefix' => '',
9
        //'encoding' => 'utf8',
10
    );

Now open core.php from the same folder and rewrite the your salt info, about at lines 225 and 230 –

Cake-logo.png

1
/**
2
 * A random string used in security hashing methods.
3
 */
4
    Configure::write('Security.salt', 'ShahjalalHossain');
5

6
/**
7
 * A random numeric string (digits only) used to encrypt/decrypt strings.
8
 */
9
    Configure::write('Security.cipherSeed', 'ShahjalalHossain');

Step 3: Let’s create our model first. As we have two tables in our database, so we will need two Models. Create Post.php in the Model folder and write your model –

1
App::uses('AppModel', 'Model');
2
/**
3
 * Post Model
4
 *
5
 */
6
class Post extends AppModel {
7

8
    public function isOwnedBy($post, $user) {
9
        return $this->field('id', array('id' => $post, 'user_id' => $user)) !== false;
10
    }
11

12
}

Now create User.php in the same folder and create the corresponding model –

1
App::uses('AppModel', 'Model');
2
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
3
/**
4
 * User Model
5
 *
6
 */
7
class User extends AppModel {
8

9
    public $validate = array(
10
        'username' => array(
11
            'required' => array(
12
                'rule' => array('notEmpty'),
13
                'message' => 'A username is required'
14
            )
15
        ),
16
        'password' => array(
17
            'required' => array(
18
                'rule' => array('notEmpty'),
19
                'message' => 'A password is required'
20
            )
21
        ),
22
        'email' => array(
23
            'email' => array(
24
                'rule'    => array('email', true),
25
                'message' => 'Please supply a valid email address.'
26
            ),
27
            'required' => array(
28
                'rule' => array('notEmpty'),
29
                'message' => 'A email is required'
30
            )
31
        ),
32
        'role' => array(
33
            'valid' => array(
34
                'rule' => array('inList', array('admin', 'author')),
35
                'message' => 'Please enter a valid role',
36
                'allowEmpty' => false
37
            )
38
        )
39
    );
40

41
    public function beforeSave($options = array()) {
42
        if (isset($this->data[$this->alias]['password'])) {
43
            $passwordHasher = new SimplePasswordHasher();
44
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
45
                $this->data[$this->alias]['password']
46
            );
47
        }
48
        return true;
49
    }
50

51
}

In the user model we have set some form validation code and in the beforeSave() method we have encrypt our password.

Step 4: We are done with our Model, so we need to create our Controller, now.


1
App::uses('AppController', 'Controller');
2
/**
3
 * Posts Controller
4
 *
5
 * @property Post $Post
6
 * @property PaginatorComponent $Paginator
7
 */
8
class PostsController extends AppController {
9

10
/**
11
 * Components
12
 *
13
 * @var array
14
 */
15
    public $components = array('Paginator');
16

17
/**
18
 * index method
19
 *
20
 * @return void
21
 */
22
    public function index() {
23
        $this->Post->recursive = 0;
24
        $this->set('posts', $this->Paginator->paginate());
25
    }
26

27
    public function visitors(){
28
        $this->Post->recursive = 0;
29
        $this->set('posts', $this->Paginator->paginate());
30
    }
31

32
/**
33
 * view method
34
 *
35
 * @throws NotFoundException
36
 * @param string $id
37
 * @return void
38
 */
39
    public function view($id = null) {
40
        if (!$this->Post->exists($id)) {
41
            throw new NotFoundException(__('Invalid post'));
42
        }
43
        $options = array('conditions' => array('Post.' . $this->Post->primaryKey => $id));
44
        $this->set('post', $this->Post->find('first', $options));
45
    }
46

47
/**
48
 * add method
49
 *
50
 * @return void
51
 */
52
    public function add() {
53
        if ($this->request->is('post')) {
54
            $this->Post->create();
55
            if ($this->Post->save($this->request->data)) {
56
                $this->Session->setFlash(__('The post has been saved.'));
57
                return $this->redirect(array('action' => 'index'));
58
            } else {
59
                $this->Session->setFlash(__('The post could not be saved. Please, try again.'));
60
            }
61
        }
62
    }
63

64
/**
65
 * edit method
66
 *
67
 * @throws NotFoundException
68
 * @param string $id
69
 * @return void
70
 */
71
    public function edit($id = null) {
72
        if (!$this->Post->exists($id)) {
73
            throw new NotFoundException(__('Invalid post'));
74
        }
75
        if ($this->request->is(array('post', 'put'))) {
76
            if ($this->Post->save($this->request->data)) {
77
                $this->Session->setFlash(__('The post has been saved.'));
78
                return $this->redirect(array('action' => 'index'));
79
            } else {
80
                $this->Session->setFlash(__('The post could not be saved. Please, try again.'));
81
            }
82
        } else {
83
            $options = array('conditions' => array('Post.' . $this->Post->primaryKey => $id));
84
            $this->request->data = $this->Post->find('first', $options);
85
        }
86
    }
87

88
/**
89
 * delete method
90
 *
91
 * @throws NotFoundException
92
 * @param string $id
93
 * @return void
94
 */
95
    public function delete($id = null) {
96
        $this->Post->id = $id;
97
        if (!$this->Post->exists()) {
98
            throw new NotFoundException(__('Invalid post'));
99
        }
100
        $this->request->allowMethod('post', 'delete');
101
        if ($this->Post->delete()) {
102
            $this->Session->setFlash(__('The post has been deleted.'));
103
        } else {
104
            $this->Session->setFlash(__('The post could not be deleted. Please, try again.'));
105
        }
106
        return $this->redirect(array('action' => 'index'));
107
    }
108
}

Each method in this folder is call their corresponding ctp file from the view.

Now, create UsersController.php in the same folder –

1
App::uses('AppController', 'Controller');
2
/**
3
 * Users Controller
4
 *
5
 * @property User $User
6
 * @property PaginatorComponent $Paginator
7
 */
8
class UsersController extends AppController {
9

10
/**
11
 * Components
12
 *
13
 * @var array
14
 */
15
    public $components = array('Paginator');
16

17

18
    public function beforeFilter() {
19
        parent::beforeFilter();
20
        $this->Auth->allow('add', 'logout');
21
    }
22

23
/**
24
 * index method
25
 *
26
 * @return void
27
 */
28
    public function index() {
29
        $this->User->recursive = 0;
30
        $this->set('users', $this->Paginator->paginate());
31
    }
32

33
/**
34
 * view method
35
 *
36
 * @throws NotFoundException
37
 * @param string $id
38
 * @return void
39
 */
40
    public function view($id = null) {
41
        if (!$this->User->exists($id)) {
42
            throw new NotFoundException(__('Invalid user'));
43
        }
44
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
45
        $this->set('user', $this->User->find('first', $options));
46
    }
47

48
/**
49
 * add method
50
 *
51
 * @return void
52
 */
53
    public function add() {
54
        if ($this->request->is('post')) {
55
            $this->User->create();
56
            if ($this->User->save($this->request->data)) {
57
                $this->Session->setFlash(__('The user has been saved.'));
58
                return $this->redirect(array('controller' => 'Posts', 'action' => 'index'));
59
            } else {
60
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
61
            }
62
        }
63
    }
64

65
/**
66
 * edit method
67
 *
68
 * @throws NotFoundException
69
 * @param string $id
70
 * @return void
71
 */
72
    public function edit($id = null) {
73
        if (!$this->User->exists($id)) {
74
            throw new NotFoundException(__('Invalid user'));
75
        }
76
        if ($this->request->is(array('post', 'put'))) {
77
            if ($this->User->save($this->request->data)) {
78
                $this->Session->setFlash(__('The user has been saved.'));
79
                return $this->redirect(array('action' => 'index'));
80
            } else {
81
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
82
            }
83
        } else {
84
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
85
            $this->request->data = $this->User->find('first', $options);
86
        }
87
    }
88

89
/**
90
 * delete method
91
 *
92
 * @throws NotFoundException
93
 * @param string $id
94
 * @return void
95
 */
96
    public function delete($id = null) {
97
        $this->User->id = $id;
98
        if (!$this->User->exists()) {
99
            throw new NotFoundException(__('Invalid user'));
100
        }
101
        $this->request->allowMethod('post', 'delete');
102
        if ($this->User->delete()) {
103
            $this->Session->setFlash(__('The user has been deleted.'));
104
        } else {
105
            $this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
106
        }
107
        return $this->redirect(array('action' => 'index'));
108
    }
109

110
    public function login() {
111
        if ($this->request->is('post')) {
112
            if ($this->Auth->login()) {
113
                return $this->redirect($this->Auth->redirect(array('controller' => 'Posts', 'action' => 'index')));
114
            }
115
            $this->Session->setFlash(__('Invalid username or password, try again'));
116
        }
117
    }
118

119
    public function logout() {
120
        //return $this->redirect($this->Auth->logout());
121
        return $this->redirect($this->Auth->logout($this->Auth->redirect(array('controller' => 'Posts', 'action' => 'visitors'))));
122
    }
123
}

Now open the AppController.php and add some code for our page redirection and authentication.

1
App::uses('Controller', 'Controller');
2

3
class AppController extends Controller {
4

5
    public $components = array(
6
        'Session',
7
        'Auth' => array(
8
            'loginRedirect' => array(
9
                'controller' => 'posts',
10
                'action' => 'index'
11
            ),
12
            'logoutRedirect' => array(
13
                'controller' => 'posts',
14
                'action' => 'visitors'
15
            )
16
        )
17
    );
18

19
    public function beforeFilter() {
20
        $this->Auth->allow('index', 'view', 'visitors');
21
    }
22

23
    public function isAuthorized($user) {
24

25
        if ($this->action === 'add') {
26
            return true;
27
        }
28

29

30
        if (in_array($this->action, array('edit', 'delete'))) {
31
            $postId = (int) $this->request->params['pass'][0];
32
            if ($this->Post->isOwnedBy($postId, $user['id'])) {
33
                return true;
34
            }
35
        }
36

37
        return parent::isAuthorized($user);
38
    }
39

40
}

Step 5: We are going to create all of our views in View folder. In this create two folder called Posts and Users.

views

Let’s work with our posts, first. Inside Posts folder create –

index.ctp

1
<div class="posts index">
2
    <h2><?php echo __('Posts'); ?></h2>
3
    <table cellpadding="0" cellspacing="0">
4
    <thead>
5
    <tr>
6
            <th><?php echo $this->Paginator->sort('id'); ?></th>
7
            <th><?php echo $this->Paginator->sort('title'); ?></th>
8
            <th><?php echo $this->Paginator->sort('body'); ?></th>
9
            <th><?php echo $this->Paginator->sort('created'); ?></th>
10
            <th><?php echo $this->Paginator->sort('modified'); ?></th>
11
            <th class="actions"><?php echo __('Actions'); ?></th>
12
    </tr>
13
    </thead>
14
    <tbody>
15
    <?php foreach ($posts as $post): ?>
16
    <tr>
17
        <td><?php echo h($post['Post']['id']); ?>&nbsp;</td>
18
        <td><?php echo h($post['Post']['title']); ?>&nbsp;</td>
19
        <td><?php echo h($post['Post']['body']); ?>&nbsp;</td>
20
        <td><?php echo h($post['Post']['created']); ?>&nbsp;</td>
21
        <td><?php echo h($post['Post']['modified']); ?>&nbsp;</td>
22
        <td class="actions">
23
            <?php echo $this->Html->link(__('View'), array('action' => 'view', $post['Post']['id'])); ?>
24
            <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $post['Post']['id'])); ?>
25
            <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $post['Post']['id']), array(), __('Are you sure you want to delete # %s?', $post['Post']['id'])); ?>
26
        </td>
27
    </tr>
28
<?php endforeach; ?>
29
    </tbody>
30
    </table>
31
    <p>
32
    <?php
33
    echo $this->Paginator->counter(array(
34
    'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
35
    ));
36
    ?>   </p>
37
    <div class="paging">
38
    <?php
39
        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
40
        echo $this->Paginator->numbers(array('separator' => ''));
41
        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
42
    ?>
43
    </div>
44
</div>
45
<div class="actions">
46
    <h3><?php echo __('Actions'); ?></h3>
47
    <ul>
48
        <li><?php echo $this->Html->link(__('New Post'), array('action' => 'add')); ?></li>
49
        <li><?php echo $this->Html->link(__('Logout'), array('controller' => 'Users', 'action' => 'logout')); ?></li>
50
    </ul>
51
</div>

add.ctp

1
<div class="posts form">
2
<?php echo $this->Form->create('Post'); ?>
3
    <fieldset>
4
        <legend><?php echo __('Add Post'); ?></legend>
5
    <?php
6
        echo $this->Form->input('title');
7
        echo $this->Form->input('body');
8
    ?>
9
    </fieldset>
10
<?php echo $this->Form->end(__('Submit')); ?>
11
</div>
12
<div class="actions">
13
    <h3><?php echo __('Actions'); ?></h3>
14
    <ul>
15

16
        <li><?php echo $this->Html->link(__('List Posts'), array('action' => 'index')); ?></li>
17
        <li><?php echo $this->Html->link(__('Logout'), array('controller' => 'Users', 'action' => 'logout')); ?></li>
18
    </ul>
19
</div>

Create edit.ctp in same folder

1
<div class="posts form">
2
<?php echo $this->Form->create('Post'); ?>
3
    <fieldset>
4
        <legend><?php echo __('Edit Post'); ?></legend>
5
    <?php
6
        echo $this->Form->input('id');
7
        echo $this->Form->input('title');
8
        echo $this->Form->input('body');
9
    ?>
10
    </fieldset>
11
<?php echo $this->Form->end(__('Submit')); ?>
12
</div>
13
<div class="actions">
14
    <h3><?php echo __('Actions'); ?></h3>
15
    <ul>
16

17
        <li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Post.id')), array(), __('Are you sure you want to delete # %s?', $this->Form->value('Post.id'))); ?></li>
18
        <li><?php echo $this->Html->link(__('List Posts'), array('action' => 'index')); ?></li>
19
        <li><?php echo $this->Html->link(__('Logout'), array('controller' => 'Users', 'action' => 'logout')); ?></li>
20
    </ul>
21
</div>

view.ctp

1
<div class="posts view">
2
<h2><?php echo __('Post'); ?></h2>
3
    <dl>
4
        <dt><?php echo __('Id'); ?></dt>
5
        <dd>
6
            <?php echo h($post['Post']['id']); ?>
7
            &nbsp;
8
        </dd>
9
        <dt><?php echo __('Title'); ?></dt>
10
        <dd>
11
            <?php echo h($post['Post']['title']); ?>
12
            &nbsp;
13
        </dd>
14
        <dt><?php echo __('Body'); ?></dt>
15
        <dd>
16
            <?php echo h($post['Post']['body']); ?>
17
            &nbsp;
18
        </dd>
19
        <dt><?php echo __('Created'); ?></dt>
20
        <dd>
21
            <?php echo h($post['Post']['created']); ?>
22
            &nbsp;
23
        </dd>
24
        <dt><?php echo __('Modified'); ?></dt>
25
        <dd>
26
            <?php echo h($post['Post']['modified']); ?>
27
            &nbsp;
28
        </dd>
29
    </dl>
30
</div>
31
<div class="actions">
32
    <h3><?php echo __('Actions'); ?></h3>
33
    <ul>
34
        <li><?php echo $this->Html->link(__('Edit Post'), array('action' => 'edit', $post['Post']['id'])); ?> </li>
35
        <li><?php echo $this->Form->postLink(__('Delete Post'), array('action' => 'delete', $post['Post']['id']), array(), __('Are you sure you want to delete # %s?', $post['Post']['id'])); ?> </li>
36
        <li><?php echo $this->Html->link(__('List Posts'), array('action' => 'index')); ?> </li>
37
        <li><?php echo $this->Html->link(__('New Post'), array('action' => 'add')); ?> </li>
38
        <li><?php echo $this->Html->link(__('Logout'), array('controller' => 'Users', 'action' => 'logout')); ?></li>
39
    </ul>
40
</div>

visitors.ctp

1
<div class="posts index">
2
    <h2><?php echo __('Posts'); ?></h2>
3
    <table cellpadding="0" cellspacing="0">
4
        <thead>
5
        <tr>
6
            <th><?php echo $this->Paginator->sort('id'); ?></th>
7
            <th><?php echo $this->Paginator->sort('title'); ?></th>
8
            <th><?php echo $this->Paginator->sort('body'); ?></th>
9
            <th><?php echo $this->Paginator->sort('created'); ?></th>
10
            <th><?php echo $this->Paginator->sort('modified'); ?></th>
11
        </tr>
12
        </thead>
13
        <tbody>
14
        <?php foreach ($posts as $post): ?>
15
            <tr>
16
                <td><?php echo h($post['Post']['id']); ?>&nbsp;</td>
17
                <td><?php echo h($post['Post']['title']); ?>&nbsp;</td>
18
                <td><?php echo h($post['Post']['body']); ?>&nbsp;</td>
19
                <td><?php echo h($post['Post']['created']); ?>&nbsp;</td>
20
                <td><?php echo h($post['Post']['modified']); ?>&nbsp;</td>
21
            </tr>
22
        <?php endforeach; ?>
23
        </tbody>
24
    </table>
25
    <p>
26
        <?php
27
        echo $this->Paginator->counter(array(
28
            'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
29
        ));
30
        ?>   </p>
31
    <div class="paging">
32
        <?php
33
        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
34
        echo $this->Paginator->numbers(array('separator' => ''));
35
        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
36
        ?>
37
    </div>
38
</div>
39
<div class="actions">
40
    <h3><?php echo __('Actions'); ?></h3>
41
    <ul>
42
        <li><?php echo $this->Html->link(__('Login'), array('controller' => 'Users', 'action' => 'login')); ?></li>
43
    </ul>
44
</div>

We are done with our posts, so now let’s work with our users. Inside Users folder create –

index.ctp

1
<div class="users index">
2
    <h2><?php echo __('Users'); ?></h2>
3
    <table cellpadding="0" cellspacing="0">
4
    <thead>
5
    <tr>
6
            <th><?php echo $this->Paginator->sort('id'); ?></th>
7
            <th><?php echo $this->Paginator->sort('username'); ?></th>
8
            <th><?php echo $this->Paginator->sort('password'); ?></th>
9
            <th><?php echo $this->Paginator->sort('role'); ?></th>
10
            <th><?php echo $this->Paginator->sort('created'); ?></th>
11
            <th><?php echo $this->Paginator->sort('modified'); ?></th>
12
            <th class="actions"><?php echo __('Actions'); ?></th>
13
    </tr>
14
    </thead>
15
    <tbody>
16
    <?php foreach ($users as $user): ?>
17
    <tr>
18
        <td><?php echo h($user['User']['id']); ?>&nbsp;</td>
19
        <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
20
        <td><?php echo h($user['User']['password']); ?>&nbsp;</td>
21
        <td><?php echo h($user['User']['role']); ?>&nbsp;</td>
22
        <td><?php echo h($user['User']['created']); ?>&nbsp;</td>
23
        <td><?php echo h($user['User']['modified']); ?>&nbsp;</td>
24
        <td class="actions">
25
            <?php echo $this->Html->link(__('View'), array('action' => 'view', $user['User']['id'])); ?>
26
            <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $user['User']['id'])); ?>
27
            <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $user['User']['id']), array(), __('Are you sure you want to delete # %s?', $user['User']['id'])); ?>
28
        </td>
29
    </tr>
30
<?php endforeach; ?>
31
    </tbody>
32
    </table>
33
    <p>
34
    <?php
35
    echo $this->Paginator->counter(array(
36
    'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
37
    ));
38
    ?>   </p>
39
    <div class="paging">
40
    <?php
41
        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
42
        echo $this->Paginator->numbers(array('separator' => ''));
43
        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
44
    ?>
45
    </div>
46
</div>
47
<div class="actions">
48
    <h3><?php echo __('Actions'); ?></h3>
49
    <ul>
50
        <li><?php echo $this->Html->link(__('New User'), array('action' => 'add')); ?></li>
51
    </ul>
52
</div>

add.ctp

1
<div class="users form">
2
<?php echo $this->Form->create('User'); ?>
3
    <fieldset>
4
        <legend><?php echo __('Add User'); ?></legend>
5
    <?php
6
        echo $this->Form->input('username');
7
        echo $this->Form->input('password');
8
        echo $this->Form->input('email');
9
        echo $this->Form->input('phone');
10
        echo $this->Form->input('role', array('type'=>'hidden', 'value'=>'admin'));
11
    ?>
12
    </fieldset>
13
<?php echo $this->Form->end(__('Submit')); ?>
14
</div>
15
<div class="actions">
16
    <h3><?php echo __('Actions'); ?></h3>
17
    <ul>
18

19
        <li>Register your details.</li>
20
    </ul>
21
</div>

edit.ctp


1
<div class="users form">
2
<?php echo $this->Form->create('User'); ?>
3
    <fieldset>
4
        <legend><?php echo __('Edit User'); ?></legend>
5
    <?php
6
        echo $this->Form->input('id');
7
        echo $this->Form->input('username');
8
        echo $this->Form->input('password');
9
        echo $this->Form->input('role');
10
    ?>
11
    </fieldset>
12
<?php echo $this->Form->end(__('Submit')); ?>
13
</div>
14
<div class="actions">
15
    <h3><?php echo __('Actions'); ?></h3>
16
    <ul>
17

18
        <li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('User.id')), array(), __('Are you sure you want to delete # %s?', $this->Form->value('User.id'))); ?></li>
19
        <li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
20
    </ul>
21
</div>

login.ctp

1
<div class="users form">
2
<?php echo $this->Session->flash('auth'); ?>
3
<?php echo $this->Form->create('User'); ?>
4
<fieldset>
5
    <legend>
6
        <?php echo __('Please enter your username and password'); ?>
7
    </legend>
8
    <?php echo $this->Form->input('username');
9
    echo $this->Form->input('password');
10
    ?>
11
</fieldset>
12
<?php echo $this->Form->end(__('Login')); ?> or, <?php echo $this->Html->link(__('Register'), array('action' => 'add')); ?>
13
</div>

view.ctp

1
<div class="users view">
2
<h2><?php echo __('User'); ?></h2>
3
    <dl>
4
        <dt><?php echo __('Id'); ?></dt>
5
        <dd>
6
            <?php echo h($user['User']['id']); ?>
7
            &nbsp;
8
        </dd>
9
        <dt><?php echo __('Username'); ?></dt>
10
        <dd>
11
            <?php echo h($user['User']['username']); ?>
12
            &nbsp;
13
        </dd>
14
        <dt><?php echo __('Password'); ?></dt>
15
        <dd>
16
            <?php echo h($user['User']['password']); ?>
17
            &nbsp;
18
        </dd>
19
        <dt><?php echo __('Role'); ?></dt>
20
        <dd>
21
            <?php echo h($user['User']['role']); ?>
22
            &nbsp;
23
        </dd>
24
        <dt><?php echo __('Created'); ?></dt>
25
        <dd>
26
            <?php echo h($user['User']['created']); ?>
27
            &nbsp;
28
        </dd>
29
        <dt><?php echo __('Modified'); ?></dt>
30
        <dd>
31
            <?php echo h($user['User']['modified']); ?>
32
            &nbsp;
33
        </dd>
34
    </dl>
35
</div>
36
<div class="actions">
37
    <h3><?php echo __('Actions'); ?></h3>
38
    <ul>
39
        <li><?php echo $this->Html->link(__('Edit User'), array('action' => 'edit', $user['User']['id'])); ?> </li>
40
        <li><?php echo $this->Form->postLink(__('Delete User'), array('action' => 'delete', $user['User']['id']), array(), __('Are you sure you want to delete # %s?', $user['User']['id'])); ?> </li>
41
        <li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?> </li>
42
        <li><?php echo $this->Html->link(__('New User'), array('action' => 'add')); ?> </li>
43
    </ul>
44
</div>

Ohh.. I almost forgot. Open routes.php from config folder and create some changes in the following lines to set our default routes

1
Router::connect('/', array('controller' => 'posts', 'action' => 'visitors'));
2
/**
3
 * ...and connect the rest of 'Pages' controller's URLs.
4
 */
5
    Router::connect('/logreg/*', array('controller' => 'posts', 'action' => 'visitors'));