Yii 之components


當我們創建一個module的時候,對應的path alias就已經創建。
比如我們定義了一個module: www

1
2
3
4
5
'modules' => array (
     'www' => array (
         'class' => 'applications.modules.www.WwwModule' ,
     ),
),

打印:
echo Yii::app()->getPathOfAlias(‘www’)
你會發現www別名已經指向我們的www模塊了。

假定在我們的www模塊下有如下目錄:
www
–components
—-ApiBase.php
–extensions
–vendors
—-Curl.php

如何加載這里面的components或者其它第三方包呢?
如下:

1
2
3
4
5
6
7
8
9
'modules' => array (
     'www' => array (
         'class' => 'applications.modules.www.WwwModule' ,
         'components' => array (
              'api' => array ( 'class' => 'www.components.ApiBase' ),
              'curl' => array ( 'class' => 'www.vendors.Curl' ),
         ),
     ),
),

那么在controller里面該如何獲取呢:
如下:

1
2
$api = $this ->getModule()->api;
$curl = $this ->getModule()->curl;

$this是當前的controller.

請注意: 我們這里沒有使用Yii::app()->curl來獲取component,因為我們是把component定義到modules里面了。
其它模塊想要使用我們的擴展怎么辦?

1
$curl = Yii::app()->getModule( 'www' )->curl;

當然對於公共的擴展,不建議定義的單個module里面, 還是放到外面的components里面定義。 然后采用Yii::app()->curl 來獲取即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM