English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Aliases
  • 时间:2024-09-17

Yii - Apases


Previous Page Next Page  

Apases help you not to hard-code absolute paths or URLs in your project. An apas starts with the @ character.

To define an apas you should call the Yii::setApas() method −

// an apas of a file path
Yii::setApas( @apas ,  /path/to/apas );
// an apas of a URL
Yii::setApas( @urlApas ,  http://www.google.com );

You can also derive a new apas from an existing one −

Yii::setApas( @pathToSomewhere ,  @apas/path/to/somewhere );

You can call the Yii::setApas() method in the entry script or in a writable property called apases in the apppcation configuration −

$config = [
    id  =>  basic ,
    basePath  => dirname(__DIR__),
    bootstrap  => [ log ],
    components  => [
       apases  => [
          @apas  =>  /path/to/somewhere ,
          @urlApas  =>  http://www.google.com ,
      ],
      //other components...
   ]
]

To resolve apas, you should call the Yii::getApas() method.

Yii predefines the following apases −

    @app − The base path of the apppcation.

    @yii − The folder where the BaseYii.php file is located.

    @webroot − The Web root directory of the apppcation.

    @web − The base URL of the apppcation.

    @runtime − The runtime path of the apppcation. Defaults to @app/runtime.

    @vendor − The Composer vendor directory. Defaults to @app/vendor.

    @npm − The root directory for npm packages. Defaults to @vendor/npm.

    @bower − The root directory for bower packages. Defaults to @vendor/bower.

Now, add a new function called actionApases() to the SiteController −

pubpc function actionApases() {
   Yii::setApas("@components", "@app/components");
   Yii::setApas("@imagesUrl", "@web/images");
   var_dump(Yii::getApas("@components"));
   var_dump(Yii::getApas("@imagesUrl"));
}

In the above code, we created two apases: @components for apppcation components and @imagesUrl for URL where we stored all apppcation images.

Type http://localhost:8080/index.php?r=site/apases, you will see the following output −

Set Apases Advertisements