当我们创建一个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 来获取即可