You are on page 1of 4

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à file modules\schedule\views\default\index.php

Sau đó thêm vào nội dung sau, file DefaultController.php

1 <?php

2  
namespace app\modules\schedule\controllers;
3

4  
use Yii;
5
use yii\web\Controller;
6
 
7
/*
8
 * Default controller
9
 * @author Ha Tuan Kiet <haanhdon@gmail.com>
10  */

11  
12
class DefaultController extends Controller
13
{
14
    public function actionIndex()
15
    {
16         Yii::$app->view->title = 'Modules trong Yii2
Framework';
17
         
18
        return $this->render('index');
19
    }
20
}<br>
21

File modules\schedule\views\default\index.php

1 <h3>Hello</h3>

2  

3 <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 <?php

2  
//phpandmysql.net
3

4  
namespace app\modules\schedule;
5
 
6
class Schedule extends \yii\base\Module
7
{
8
    public $controllerNamespace =
'app\modules\schedule\controllers';
9

10  
    public function init()
11
    {
12
        parent::init();
13
 
14
        // custom initialization code goes here
15
    }
16 }

17

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 'modules' => [

2         'schedule' => [

3             'class' => 'app\modules\schedule\Schedule',

4         ],

    ],
5

Modules khai báo cùng cấp với components Chi tiết hơn bạn xem code dưới

1 return [

    'id' => 'app-frontend',


2
    'basePath' => dirname(__DIR__),
3
    'bootstrap' => ['log'],
4
    'controllerNamespace' => 'frontend\controllers',
5
6
    'modules' => [
7         'schedule' => [
8             'class' => 'app\modules\schedule\Schedule',

9         ],

10     ],

11     'components' => [

        'request' => [
12
            'baseUrl' => '/',
13
        ],
14

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.

You might also like