Skip to content

Yii中别名系统梳理 #5

Description

@erichua23

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';
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions