1.别名的定义规则
别名用来表示文件路径和 URL,这样就避免了在代码中硬编码一些绝对路径和 URL。 一个别名必须以 @ 字符开头,以区别于传统的文件路径和 URL。 没有前导 @ 定义的别名将以 @ 字符作为前缀。
在配置文件中定义别名
2.配置文件main-local.php中定义
'aliases' => [
'@lincheng' => '/path/to/foo',
],
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'BbrwMnBSkmMSoFKXSxr9a8DKal4jHaug',
],
'view' => [
],
'i18n' => [
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
],
'sms123' => [
'class' => 'backend\components\Sms'
]
],
// 'controllerNamespace' => 'backend\control',
'defaultRoute' => 'test',
'viewPath' => '@backend/template',
// 'layoutPath' => '@backend/template/public'
// 'layout' => false
// 'layout' => 'common'
'language' => 'zh-CN',
'modules' => [
'shop' => [
'class' => 'backend\modules\shop\Module',
]
],
'aliases' => [
'@lincheng' => '/path/to/foo',
],
];
3.控制器中获取和定义
public function actionIndex18() {
echo "<pre>";
// print_r(Yii::$app->view);
// print_r(Yii::$app->get('view'));
// print_r(Yii::$app->getView());
// print_r(Yii::$app->get('db'));
// Yii::$app->sms123->send('13900000000');
// backend/web/css/site.css
// backend/web/css/index.css
// backend/web/js/index.js
Yii::setAlias('@cssPath', '/backend/web/css/');
Yii::setAlias('@my.css', '@cssPath/index.css');
echo Yii::getAlias('@cssPath/index.css');
echo "<br/>";
echo "<br/>";
echo Yii::getAlias('@runtime');
}