Ngu ngơ học làm web (x35) - CakePHP2 - Đăng kí người dùng

Tiếp theo của: Ngu ngơ học làm web (x34) - CakePHP2 – Hàm ràng buộc tại model, hashing, callback
-----

Phần x35. CakePHP2 – Đăng kí người dùng


Xem (clip số 36bt – chickenrainshop):

Viết link để hiển thị form đăng kí người dùng:

[Layouts\default.ctp]

<div class="panel panel-info">
            <h4 class="panel-heading"><span class="glyphicon glyphicon-user"></span><small> Xin chào <strong>khách</strong></small></h4>
            Nhấn vào <?php echo $this->Html->link('đây','/login'); ?> để đăng nhập.<br>
            Nếu bạn chưa có tài khoản hãy đăng ký tại <?php echo $this->Html->link('đây', '/dang-ky') ?>.
</div>

Vào UsersController.php để viết hàm register:

[UsersController.php]

            public function register() {
                        $this->set('title_for_layout', 'Đăng ký');
            }

Vào routes.php để thực hiện route cho chức năng ‘dang-ky’:

[routes.php]

Router::connect('/doi-mat-khau', ['controller' => 'users', 'action' => 'changePassword']);
Router::connect('/dang-ky', ['controller' => 'users', 'action' => 'register']);
Router::connect('/logout', ['controller' => 'users', 'action' => 'logout']);

Cho phép người dùng thực hiện chức năng đăng ký khi chưa đăng nhập:

[AppController.php action beforeFilter()]

public function beforeFilter() {
                        $this->Auth->allow('menu', 'index', 'register');
                        $this->set('userInfo', $this->getUser());
            }

Tạo view cho action register:

[View\Users\register.ctp]

<div class="panel panel-info">
            <h4 class="panel-heading"><span class="glyphicon glyphicon-user"> Đăng ký</span></h4>
                        <?php //echo $this->Flash->render('auth'); ?>
                        <?php echo $this->element('errors'); ?>
                        <?php echo $this->Flash->render(); ?>
                        <?php echo $this->Form->create('User', ['class' => 'form-horizontal', 'novalidate' => true, 'inputDefaults' => ['label' => false]]); ?>
                                    <div class="control-group">
                                                <label class="control-label" for="inputLastName">Last Name</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('lastname', ['placeholder' => 'Họ']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <label class="control-label" for="inputFirstName">First Name</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('firstname', ['placeholder' => 'Tên', 'type' => 'text']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <label class="control-label" for="inputUserName">Username</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('username', ['placeholder' => 'Tên đăng nhập', 'type' => 'text']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <label class="control-label" for="inputEmail">Email</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('email', ['placeholder' => 'Địa chỉ email', 'type' => 'text']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <label class="control-label" for="inputPassword">Password</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('password', ['placeholder' => 'Mật khẩu', 'type' => 'password']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <label class="control-label" for="inputConfirmPassword">Confirm Password</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('confirmPassword', ['placeholder' => 'Xác nhận mật khẩu', 'type' => 'password']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <label class="control-label" for="inputAddress">Address</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('address', ['placeholder' => 'Địa chỉ', 'type' => 'text', 'error' => false]); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <label class="control-label" for="inputPhone">Phone</label>
                                                <div class="controls">
                                                            <?php echo $this->Form->input('phone', ['placeholder' => 'Số điện thoại', 'type' => 'number', 'error' => false]); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <div class="controls">
                                                            <?php echo $this->Form->input('agree', ['label' => false, 'type' => 'checkbox', 'div' => false, 'after' => '<label>Tôi đồng ý với các điều khoản đã đưa ra.</label>']); ?>
                                                </div>
                                    </div>
                                    <div class="control-group">
                                                <div class="controls">
                                                            <?php echo $this->Form->button('Đăng ký', ['type' => 'submit', 'class' => 'col-lg-2 btn btn-primary']); ?>
                                                </div>
                                    </div>
                        <?php echo $this->Form->end(); ?>
</div>

Viết action register() trong UsersController.php:

            public function register() {
                        $this->set('title_for_layout', 'Đăng ký');
                        if ($this->request->is('post')) {
                                    if ($this->request->data['User']['agree'] == 1) {
                                                $this->User->create();
                                                $this->request->data['User']['group_id'] = 2;
                                                if ($this->User->save($this->request->data)) {
                                                            $this->Flash->success('Đã đăng ký thành công', ['params' => ['class' => 'alert alert-success']]);
                                                            $this->Auth->login();
                                                            $this->redirect($this->Auth->redirect());
                                                } else {
                                                            $this->Flash->error('Đăng ký chưa được, vui lòng thử lại!', ['params' => ['class' => 'alert alert-error']]);
                                                }
                                    } else {
                                                $this->Flash->error('Bạn cần đồng ý với các thỏa thuận để tiếp tục!', ['params' => ['class' => 'alert alert-error']]);         
                                    }
                                   
                        }
            }

Chỉnh lại một số trường trong Model\User.php:

'username' => array(
                                    'notBlank' => array(
                                                'rule' => array('notBlank'),
                                                'message' => 'Tên đăng nhập không được để trống',
                                                //'allowEmpty' => false,
                                                //'required' => false,
                                                //'last' => false, // Stop validation after this rule
                                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                                    ),
                                    'unique' => array(
                                                'rule' => 'isUnique',
                                                'message' => 'Tên đăng nhập đã tồn tại',
                                    ),
                        ),
'email' => array(
                                    'email' => array(
                                                'rule' => array('email'),
                                                'message' => 'Địa chỉ email không hợp lệ.',
                                                //'allowEmpty' => false,
                                                //'required' => false,
                                                //'last' => false, // Stop validation after this rule
                                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                                    ),
                                    'unique' => array(
                                                'rule' => 'isUnique',
                                                'message' => 'Địa chỉ email đã tồn tại.'
                                    ),
                        ),
'firstname' => array(
                                    'notBlank' => array(
                                                'rule' => array('notBlank'),
                                                'message' => 'Tên không được để trống.',
                                                //'allowEmpty' => false,
                                                //'required' => false,
                                                //'last' => false, // Stop validation after this rule
                                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                                    ),
                        ),
                        'lastname' => array(
                                    'notBlank' => array(
                                                'rule' => array('notBlank'),
                                                'message' => 'Họ không được để trống.',
                                                //'allowEmpty' => false,
                                                //'required' => false,
                                                //'last' => false, // Stop validation after this rule
                                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                                    ),

                        ),
-----------
Cập nhật 5/7/2017
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web