Yii2 模組module筆記

來源:互聯網
上載者:User

標籤:

包含內容:

使用GII建立module

建立子模組

在其他控制器中調用模組的操作(action)

 

1. 使用Gii工具建立module

 

注意模組的路徑,我們沒有寫backend\modules\Article。多了一層article目錄是為了防止如果有多個模組共用同一檔案。

 

2. 在backend\config\main.php中添加配置代碼。

  ‘modules‘ => [        ‘article‘ => [            ‘class‘ => ‘backend\modules\article\Article‘,        ],    ],

 

3. 訪問

http://你的項目後台地址/article/default/index

 

4. 調用模組中的操作

在後台某個控制器檔案,如backend\controllers\CarController.php 中添加調用模組動作的代碼

    public function actionIndex()    {        // 擷取子模組        $arcileModule = Yii::$app->getModule(‘article‘);        // 調用子模組操作        $arcileModule->runAction(‘default/index‘);        ........

 

5. 建立子模組。在article下建立留言comment模組

Module Class填寫:backend\modules\article\modules\comment\Comment

 

6. 添加配置資訊

開啟backend\modules\article\Article.php。在init方法內加入

    public function init()    {        parent::init();        $this->modules = [              ‘comment‘ => [                ‘class‘ => ‘backend\modules\article\modules\comment\Comment‘,            ],        ];        // custom initialization code goes here    }

 

7. 訪問

http://你的項目後台地址/article/comment/default/index

若要直接存取,http://你的項目後台地址/comment/default/index

需要將剛才的配置資訊加入到backend\config\main.php下面的設定檔裡

 

Yii2 模組module筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.