0

tìm hiểu về zendframework 2 : tự tạo module album

Về zend framework 2

Các bước để tạo module trong zend framework 2

  • có rất nhiều cách để tạo một module trong zend framework 2. Nhưng để tìm hiểu rõ hơn về cấu trúc cũng như cách hoạt động của một module trong zendframework 2 chúng ta sẽ tự tạo một module bằng tay thay vì sử dụng các tool auto generate.

Cài đặt module Album

  • ZF2 là một framework theo mô hình thiết kế mvc. chính vì vậy trong một module cũng phải có tối thiểu 3 thành phần chính là mvc
  • tạo cấu trúc thư mục như sau:

module.png

    • config: chứa các file config của module
    • src: là nơi lưu trữ các file code của module: controller, model, view ...
    • file module.php là file cấu hình cho module.
  • Chỉnh sửa file application.config.php để project nhận module

  •   'modules' => array(
          'Application',
          'Album',
      ),
    
    

### Cơ chế nạp cấu hình của ZF2
- việc nạp cấu hình cho module trong ZF2 được định nghĩa và quản lý bởi file Module.php được đặt trong thư mục gốc của module

```php

<?php
namespace Album;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                     __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Album\Model\AlbumTable' =>  function($sm) {
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
}

  • Module Manager sẽ tự động gọi các phương thức getConfig()getAutoloaderConfig(). Module Manager có thể quản lý nhiều module, và chịu trách nhiệm khởi tạo và nạp module đó vào hệ thống. Toàn bộ các module đều được duyệt qua một lượt trong quá trình khởi tạo
    • getConfig(): trả về mảng cấu hình controller, view, router,... của module. Trong module này ta sẽ tạo ra 1 file module.config.php được đặt trong thư mục config của module:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Album\Controller\Album' => 'Album\Controller\AlbumController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'album' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/album[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
        ),
    ),
);

-- Có thể thấy trên file config trên ta đã định nghĩa cho hầu hết các thành phần trực tiếp tạo nên module như thư mục chứa controller, các router của module, và view của module.

    • getAutoloaderConfig() : trả ra một mảng cấu hình những đường dẫn chứa các className/ namsapce.

Tạo các thành phần của module

Tạo các file như sau: Selection_007.png


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí