Thứ Năm, 29 tháng 9, 2016

Yii2 phần 7 - Modules trong yii2 framework

Khi trang web của chúng ta có nhiều modules, lúc này nếu để lẫn lộn tất cả các controller của từng module lại với nhau trong cùng một thư mục sẽ khiến ứng dụng bị rối, code không được tổ chức rõ ràng. Phần này chúng ta sẽ thao tác chia các controllers và views theo từng modules riêng, ví dụ như users, news, products... thành mỗi modules riêng.
Một modules trong yii2 framework bao gốm có controllers và views như một mô hình MVC bình thường, controllers sẽ chứa các controller và views chứa các file view theo từng controller.

1. Thao tác với module, tạo các modules

Ta tiến hành tạo folder modules trong folder frontend, ví dụ ở đây ta sẽ tạo ra module schedule, trong module schedule ta tạo 2 folder là controllers, views và file Schedule.php

- Tạo file modules\schedule\controllers\DefaultController.php và filemodules\schedule\views\default\index.php
Modules trong yii2 framework 

Sau đó thêm vào nội dung sau, file DefaultController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
namespace app\modules\schedule\controllers;
 
use Yii;
use yii\web\Controller;
 
/*
 * Default controller
 * @author Ha Tuan Kiet <haanhdon@gmail.com>
 */
 
class DefaultController extends Controller
{
    public function actionIndex()
    {
        Yii::$app->view->title = 'Modules trong Yii2 Framework';
         
        return $this->render('index');
    }
}<br>

File modules\schedule\views\default\index.php
1
2
3
<h3>Hello</h3>
 
<p>Module schedule - Yii2 Framework</p>


Mở file frontend\modules\schedule\Schedule.php lên ta thêm vào nội dung sau
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
 
//phpandmysql.net
 
namespace app\modules\schedule;
 
class Schedule extends \yii\base\Module
{
    public $controllerNamespace = 'app\modules\schedule\controllers';
 
    public function init()
    {
        parent::init();
 
        // custom initialization code goes here
    }
}


Ta đã tạo xong modules schedule, tuy nhiên lúc này ta chưa thể chạy được module này vì chưa khai báo nó trong file config\main.php


2. Khai báo modules


Trong file fronend\config\main.php ta thêm vào đoạn code khai báo module
1
2
3
4
5
'modules' => [
        'schedule' => [
            'class' => 'app\modules\schedule\Schedule',
        ],
    ],
Modules khai báo cùng cấp với components Chi tiết hơn bạn xem code dưới
1
2
3
4
5
6
7
8
9
10
11
12
13
14
return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'modules' => [
        'schedule' => [
            'class' => 'app\modules\schedule\Schedule',
        ],
    ],
    'components' => [
        'request' => [
            'baseUrl' => '/',
        ],

Oke, như vậy là đã xong một module, bạn có thể tạo thêm nhiều module mà bạn muốn, lúc này ta có thể chạy module schedule theo đường dẫn
1
http://yiiadvanced/schedule

Theo đúng thứ tự khi chạy một module sẽ là module -> controller -> action. Ở trong Yii2 framework thì DefaultController của một module sẽ chạy đầu tiên, và trong một controller thì actionIndex sẽ chạy đầu tiên.

Nếu viết đầy đủ ra thì để chạy module schedule và đến actionIndex của DefaultController thì url sẽ là
1
http://yiiadvanced/schedule/default/index

Đây là kết quả khi ta chạy module schedule.

Module schedule - Yii2 framework


EmoticonEmoticon