在应用程序配置文件中的Yii2中设置别名 [英] Setting aliases in Yii2 within the app config file

查看:372
本文介绍了在应用程序配置文件中的Yii2中设置别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Yii2中设置alias,但对于放置在应用程序配置文件中的以下代码却获得了Invalid Parameter / Invalid path alias:

I'm trying to set an alias in Yii2 but I'm getting a Invalid Parameter / Invalid path alias for the below code that is placed in the app config file:

'aliases' => [
    // Set the editor language dir
    '@editor_lang_dir' => '@webroot/scripts/sceditor/languages/',       
],

如果我删除@,它将起作用.

If I remove the @ it works.

我注意到您可以这样做:

I noticed you can do this:

Yii::setAlias('@foobar', '@foo/bar');

...但是我希望在应用程序配置文件中进行设置.这不可能吗?如果可以,怎么办?

...but I would prefer to set it within the app config file. Is this not possible? If so, how?

推荐答案

config文件夹中创建文件aliases.php.并把这个:

In config folder create file aliases.php. And put this:

Yii::setAlias('webroot', dirname(dirname(__DIR__)) . '/web');
Yii::setAlias('editor_lang_dir', '@webroot/scripts/sceditor/languages/');

index.php文件的web文件夹中,放置: require(__DIR__ . '/../config/aliases.php');

In web folder in index.php file put: require(__DIR__ . '/../config/aliases.php');

之前:

(new yii\web\Application($config))->run();

如果在视图文件中运行echo:

If run echo in view file:

echo Yii::getAlias('@editor_lang_dir');

显示如下:

C:\OpenServer\domains\yii2_basic/web/scripts/sceditor/languages/

这篇关于在应用程序配置文件中的Yii2中设置别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆