Ngu ngơ học làm web (x52) - CakePHP2 - Tạo các trang tĩnh

Tiếp theo của: Ngu ngơ học làm web (x51) - CakePHP2 – Khóa tài khoản
-----

Phần 52. CakePHP2 – Tạo các trang tĩnh


Xem (clip số 50 – chickenrainshop):

- Tạo các trang tĩnh, PagesController (50)

- Khai báo controller không sử dụng model (2":06)

- Xử lý tab active (10":31)

Tạo các trang tĩnh:

Để tạo các trang tĩnh, viết các action cho các trang tĩnh trong PagesController. PagesController được tạo sẵn khi tạo một dự án bằng CakePHP.

[PagesController.php]

Vô hiệu action display() vì không cần dùng tới.

Viết thêm các action: contact(), about(), policy().

public function contact() {
                        $this->set('title_for_layout', 'Liên hệ');
                        if ($this->request->is('post')) {
                                    $this->Page->set($this->request->data);
                                    if ($this->Page->validates()) {
                                                App::uses('CakeEmail', 'Network/Email');
                                                $email = new CakeEmail();
                                                $email->config('smtp')
                                                            ->emailFormat('both')
                                                            ->subject('Đã có một liên hệ mới trên trang Web')
                                                            ->send('Người gửi:'.$this->request->data['Page']['fullname'].'<br/>Email:'.$this->request->data['Page']['email']);
                                                $this->Flash->success('Cám ơn bạn đã gửi email, chúng tôi sẽ trả lời sớm nhất có thể!', ['params' => ['class' => 'alert alert-success']]);          
                                                $this->request->data['Page'] = null;
                                    } else {
                                                $this->set('errors', $this->Page->validationErrors);
                                    }
                        }
            }

            public function about() {
                        $this->set('title_for_layout', 'Trang about');
            }

            public function policy() {
                        $this->set('title_for_layout', 'Điều khoản');
            }
}

[View\Pages\about.ctp]

Trang about!

[View\Pages\policy.ctp]

Trang policy!

[View\Pages\contact.ctp]

<div class="panel panel-info">
            <h4 class="panel-heading"><i class="glyphicon glyphicon-user"></i>Liên hệ</h4>
            <p>Điền các thông tin dưới đây để liên hệ với chúng tôi</p>
            <hr>
            <?php echo $this->Flash->render(); ?>
            <?php echo $this->element('errors'); ?>
                        <?php echo $this->Form->create('Page', ['class' => 'form-horizontal', 'inputDefaults' => ['label' => false, 'error' => false, 'required' => true]]); ?>
                        <div class="control-group">
                                    <label for="inputFullname" class="control-label">Họ và Tên</label>
                                    <div class="controls">
                                                <?php echo $this->Form->input('fullname', ['placeholder' => 'Họ và Tên']); ?>
                                    </div>
                        </div>
                        <div class="control-group">
                                    <label for="inputEmail" class="control-label">Email</label>
                                    <div class="controls">
                                                <?php echo $this->Form->input('email', ['placeholder' => 'Nhập địa chỉ email']); ?>
                                    </div>
                        </div>
                        <div class="control-group">
                                    <label for="inputContent" class="control-label">Nội dung</label>
                                    <div class="controls">
                                                <?php echo $this->Form->input('content', ['placeholder' => 'Nội dung liên hệ', 'type' => 'textarea']); ?>
                                    </div>
                        </div>
                        <hr>
                        <div class="form-actions">
                                    <?php echo $this->Form->button('Gửi', ['type' => "submit", 'class' => "col-lg-2 btn btn-primary"]); ?>
                        </div>
                        <?php echo $this->Form->end(); ?>
</div>

[Model\Page.php]

<?php
App::uses('AppModel', 'Model');

class Page extends AppModel {
            public $useTable = false;
            public $validate = array(
                        'fullname' => array(
                                    'notblank' => array(
                                    'rule' => array('notBlank'),
                                    'message' => 'Họ và tên không được để trống',
                                    )
                        ),
                        'email' => array(
                                    'rule' => array('email'),
                                    'message' => 'Email không được để trống',
                        ),
                        'content' => array(
                                    'notblank' => array(
                                                'rule' => array('notblank'),
                                                'message' => 'Nội dung không được để trống',
                                    ),
                                    'minLength' => array(
                                                'rule' => array('minLength', 10),
                                                'message' => 'Nội dung quá ngắn'
                                    )
                        ),
            );
}

?>

Thực hiện route cho các trang: about, contact, policy.

[routes.php]

Router::connect('/dieu-khoan', ['controller' => 'pages', 'action' => 'policy']);
Router::connect('/ve-trang-web', ['controller' => 'pages', 'action' => 'about']);
Router::connect('/lien-he', ['controller' => 'pages', 'action' => 'contact']);

Đặt các link cho các trang: about, contact, policy.

[View\Layouts\default.ctp]

<ul class="nav navbar-nav">
                                    <li class="active"><?php echo $this->Html->link('Sách mới', '/sach-moi') ?></li>
                                    <li><?php echo $this->Html->link('Sách bán chạy', '/sach-ban-chay'); ?></li>
                                    <li><?php echo $this->Html->link('Liên hệ', '/lien-he'); ?></li>
                                    <li><?php echo $this->Html->link('Về chúng tôi', '/ve-trang-web'); ?></li>
                                    <li><?php echo $this->Html->link('Điều khoản', '/dieu-khoan'); ?></li>
</ul>

Xử lý cho menu khi một mục được active:

[View\Layouts\default.ctp]

<div class="navbar-collapse collapse" id="mainmenu">
                                    <ul class="nav navbar-nav">
                                    <?php if ($this->request->params['action'] == 'latest_books'): ?>
                                                                        <li class="active">
                                                            <?php else: ?>
                                                                        <li>
                                    <?php endif ?>
                                    <?php echo $this->Html->link('Sách mới', '/sach-moi') ?>
                                </li>
                                <?php if ($this->request->params['action'] == 'bestSelling'): ?>
                                                                        <li class="active">
                                                            <?php else: ?>
                                                                        <li>
                                    <?php endif ?>
                                    <?php echo $this->Html->link('Sách bán chạy', '/sach-ban-chay'); ?>
                                    </li>
                                    <?php if ($this->request->params['action'] == 'contact'): ?>
                                                                        <li class="active">
                                                            <?php else: ?>
                                                                        <li>
                                    <?php endif ?>
                                    <?php echo $this->Html->link('Liên hệ', '/lien-he'); ?>
                                </li>
                                <?php if ($this->request->params['action'] == 'about'): ?>
                                                                        <li class="active">
                                                            <?php else: ?>
                                                                        <li>
                                    <?php endif ?>
                                    <?php echo $this->Html->link('Về chúng tôi', '/ve-trang-web'); ?>
                        </li>
                        <?php if ($this->request->params['action'] == 'policy'): ?>
                                                                        <li class="active">
                                                            <?php else: ?>
                                                                        <li>
                                    <?php endif ?>
                                    <?php echo $this->Html->link('Điều khoản', '/dieu-khoan'); ?>
                        </li>
                                    </ul>
                                    <ul class="nav navbar-nav pull-right">
                                                <?php
                                                            echo $this->Form->create('Book', [
                                                                        'url' => ['action' => 'get_keyword'],
                                                                        'class' => 'navbar-form search'
                                                                        ]);
                                                            echo $this->Form->input('keyword',[
                                                                        'label' => '',
                                                                        'style' => 'width: 200px',
                                                                        'placeholder' => 'Tìm kiếm...'
                                                                        ]);
                                                            echo $this->Form->end();
                                                ?>
                                    </ul>

                        </div>
-----------
Cập nhật 13/8/2017
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web
Ngu ngơ học làm web (x53) - CakePHP2 - ACL, AROS, ACOS