Ngu ngơ học làm web (x45) - CakePHP2 - Tạo helper, Admin books

Tiếp theo của: Ngu ngơ học làm web (x44) - CakePHP2 – Dependent, confirm, moveUp, moveDown
-----

Phần x45. CakePHP2 – Tạo helper, Admin books


Xem (clip số 43c – chickenrainshop):

- find(threaded) (43c) (1":34)

- Tạo mới một helper (2":08)

- Hiển thị theo cấu trúc cây phân cấp (4":37)

Hiển thị danh mục sách ở trang chủ theo đúng cấu trúc cây (chứ không phải theo tên):

[CategoriesController.php]

public function menu() {
                        if ($this->request->is('requested')) {
                                    $categories = $this->Category->find('threaded', [
                                                'recursive' => -1,
                                                'order' => ['lft' => 'asc']
                                    ]);
                                    return $categories;
                        }
            }

Tạo một helper:

[View\Helper\DisplayHelper.php]

<?php
class DisplayHelper extends AppHelper {
            public function menu($categories, $text = null) {
                        $text .= '<ul>';
                        foreach ($categories as $category) {
                                    $text .= '<li>'.$category['Category']['name'].'</li>';
                                    if (!empty($category['children'])) {
                                                $text = $this->menu($category['children'], $text);
                                    }
                        }
                        $text .= '</ul>';
                        return $text;
            }
}          
?>

Khai báo để sử dụng DisplayHelper:

[AppController.php]

public $helpers = array('Display');
public function beforeFilter() {

Sửa lại [DisplayHelper.php]

<?php
class DisplayHelper extends AppHelper {
            public $helpers = ['Html'];
            public function menu($categories, $text = null) {
                        $text .= '<ul>';
                        foreach ($categories as $category) {
                                    $text .= '<li>'.$this->Html->link($category['Category']['name'], '/danh-muc/'.$category['Category']['slug']).'</li>';
                                    if (!empty($category['children'])) {
                                                $text = $this->menu($category['children'], $text);
                                    }
                        }
                        $text .= '</ul>';
                        return $text;
            }
}          
?>

Xem (clip số 44 – chickenrainshop):

- Tích hợp trình soạn thảo CKEditor (44)

- include tập tin javascript vào view (4":08)

- Sửa lỗi tiếng Việt trong CKEditor (7":05)

- Sử dụng block để gọi javascript (12":10)

Viết mã cho màn hình /admin/books:

Viết action admin_index(),

[BooksController.php]

public function admin_index() {
                        $this->set('title_for_layout', 'Book Admin');
            }

Route cho /admin/books:

[routes.php]

Router::connect('/admin/books', ['controller' => 'books', 'action' => 'index', 'admin' => true]);

Viết mã cho view admin_index:

[admin_index.ctp]

<?php //pr($books); ?>
<div class="books index">
            <h2><?php echo __('Sách'); ?></h2>
            <table cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                                    <th><?php echo $this->Paginator->sort('Danh mục'); ?></th>
                                    <th><?php echo $this->Paginator->sort('Tên sách'); ?></th>
                                    <th><?php echo $this->Paginator->sort('Ảnh'); ?></th>
                                    <th><?php echo $this->Paginator->sort('Giá bán'); ?></th>
                                    <th><?php echo $this->Paginator->sort('Ngày tạo'); ?></th>
                                    <th><?php echo $this->Paginator->sort('Ngày sửa'); ?></th>
                                    <th class="actions"><?php echo __('Actions'); ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($books as $book): ?>
            <tr>
                        <td>
                                    <?php echo $book['Category']['name']; ?>&nbsp;
                        </td>
                        <td><?php echo $book['Book']['title']; ?>&nbsp;</td>
                        <td><?php echo $this->Html->image($book['Book']['image'], ['height' => 150, 'width' => 100]); ?>&nbsp;</td>
                        <td><?php echo $this->Number->currency($book['Book']['sale_price'],' VND',['places' => 0,'wholePosition' => 'after']); ?> &nbsp;</td>
                        <td><?php echo $book['Book']['created']; ?>&nbsp;</td>
                        <td><?php echo $book['Book']['modified']; ?>&nbsp;</td>
                        <td class="actions">
                                    <?php echo $this->Html->link(__('View'), array('action' => 'view', $book['Book']['id'])); ?>
                                    <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $book['Book']['id'])); ?>
                                    <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $book['Book']['id']), array('confirm' => __('Bạn có đồng ý xóa cuốn sách không?'))); ?>
                        </td>
            </tr>
<?php endforeach; ?>
            </tbody>
            </table>
            <p>
            <?php
            echo $this->Paginator->counter(array(
                        'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
            ));
            ?>       </p>
            <div class="paging">
            <?php
                        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
                        echo $this->Paginator->numbers(array('separator' => ''));
                        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
            ?>
            </div>
</div>

Viết tiếp mã cho action admin_index(),

[BooksController.php]

public function admin_index() {
                        $this->paginate = [
                                    'fields' => ['id', 'category_id', 'title', 'slug', 'image', 'sale_price', 'created', 'modified' ],
                                    'order' => ['created' => 'desc'],
                                    'limit' => 10,
                                    'contain' => ['Category' => ['name', 'slug']],
                        ];
                        $books = $this->paginate();
                        $this->set('books', $books);
                        $this->set('title_for_layout', 'Book Admin');
            }

Trong [BooksController.php] đổi tên action edit() thành admin_edit().


Trong view Books đổi tên edit.ctp thành admin_edit.ctp.
-----------
Cập nhật 27/7/2017
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web