23lab / 23lab.github.com

23lab.github.com
blog.23lab.com
0 stars 0 forks source link

Yii中别名系统梳理 #5

Open erichua23 opened 11 years ago

erichua23 commented 11 years ago

Root Alias

* system: refers to the Yii framework directory;
* zii: refers to the Zii library directory;
* application: refers to the application's base directory; (/protected/)
* webroot: refers to the directory containing the entry script file(index.php).
* ext: refers to the directory containing all third-party extensions.

Importing Classes

Yii::import('system.web.CController');

Using Class Map

To pre-import a set of classes, the following code must be executed before CWebApplication::run() is invoked:

Yii::$classMap=array(
    'ClassName1' => 'path/to/ClassName1.php',
    'ClassName2' => 'path/to/ClassName2.php',
    ......
);

Importing Directories

Yii::import('system.web.*');

Namespace

A path alias is merely a convenient way of naming a file or directory. It has nothing to do with a namespace.

Namespaced Controllers

Using controllerMap

protected/config/main.php

// adding "mynamespace" namespace
Yii::setPathOfAlias('mynamespace', '/var/www/common/mynamespace/');

return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'My Web Application',

    'controllerMap' => array(
        'test' => '\mynamespace\controllers\TestController',
    ),
);

controller code should be properly namespaced:

// define namespace:
namespace mynamespace\controllers;

// since class is now under namespace, global namespace
// should be referenced explicitly using "\":
class TestController extends \CController
{
    public function actionIndex()
    {
        echo 'This is TestController from \mynamespace\controllers';
    }
}
erichua23 commented 11 years ago

http://www.yiiframework.com/doc/guide/1.1/en/basics.namespace