在应用程序配置文件中的 Yii2 中设置别名
Setting aliases in Yii2 within the app config file
我正在尝试在 Yii2
中设置一个 alias
,但我得到了一个 Invalid Parameter / Invalid path alias
用于放置在应用程序配置文件中的以下代码:
'aliases' => [
// Set the editor language dir
'@editor_lang_dir' => '@webroot/scripts/sceditor/languages/',
],
如果我删除 @
它会起作用。
我注意到你可以这样做:
Yii::setAlias('@foobar', '@foo/bar');
...但我更愿意在应用程序配置文件中设置它。这不可能吗?如果可以,怎么做?
在 config
文件夹中创建文件 aliases.php
。并把这个:
Yii::setAlias('webroot', dirname(dirname(__DIR__)) . '/web');
Yii::setAlias('editor_lang_dir', '@webroot/scripts/sceditor/languages/');
在 web
文件夹中 index.php
文件中放入:
require(__DIR__ . '/../config/aliases.php');
之前:
(new yii\web\Application($config))->run();
如果运行 echo
在视图文件中:
echo Yii::getAlias('@editor_lang_dir');
这样显示:
C:\OpenServer\domains\yii2_basic/web/scripts/sceditor/languages/
@webroot
别名此时不可用,它是在申请期间定义的 bootstrap :
https://github.com/yiisoft/yii2/blob/2.0.3/framework/web/Application.php#L60
不需要自己定义这个别名,你应该简单地使用另一个:
'aliases' => [
// Set the editor language dir
'@editor_lang_dir' => '@app/web/scripts/sceditor/languages/',
],
Yii2基础应用
要在配置文件中设置,请将其写入 $config 数组
'aliases' => [
'@name1' => 'path/to/path1',
'@name2' => 'path/to/path2',
],
参考:http://www.yiiframework.com/doc-2.0/guide-structure-applications.html
但如前所述here、
@yii 别名是在您将 Yii.php 文件包含在您的入口脚本中时定义的。其余别名在应用应用程序配置时在应用程序构造函数中定义。
如果你需要使用预定义的别名,写一个组件并link它在配置bootstrap数组
namespace app\components;
use Yii;
use yii\base\Component;
class Aliases extends Component
{
public function init()
{
Yii::setAlias('@editor_lang_dir', Yii::getAlias('@webroot').'/scripts/sceditor/languages/');
}
}
在配置文件中,将 'app\components\Aliases' 添加到 bootstrap 数组
'bootstrap' => [
'log',
'app\components\Aliases',
],
改进@vitalik_74的回答
你可以将它放在 config/web.php 中(如果你使用的是基本的 yii 应用程序,我不确定高级版本中的主要配置文件,但相同适用,只需将要求放在主配置文件中),以便它缩短为:
require(__DIR__ . '/aliases.php');
我正在尝试在 Yii2
中设置一个 alias
,但我得到了一个 Invalid Parameter / Invalid path alias
用于放置在应用程序配置文件中的以下代码:
'aliases' => [
// Set the editor language dir
'@editor_lang_dir' => '@webroot/scripts/sceditor/languages/',
],
如果我删除 @
它会起作用。
我注意到你可以这样做:
Yii::setAlias('@foobar', '@foo/bar');
...但我更愿意在应用程序配置文件中设置它。这不可能吗?如果可以,怎么做?
在 config
文件夹中创建文件 aliases.php
。并把这个:
Yii::setAlias('webroot', dirname(dirname(__DIR__)) . '/web');
Yii::setAlias('editor_lang_dir', '@webroot/scripts/sceditor/languages/');
在 web
文件夹中 index.php
文件中放入:
require(__DIR__ . '/../config/aliases.php');
之前:
(new yii\web\Application($config))->run();
如果运行 echo
在视图文件中:
echo Yii::getAlias('@editor_lang_dir');
这样显示:
C:\OpenServer\domains\yii2_basic/web/scripts/sceditor/languages/
@webroot
别名此时不可用,它是在申请期间定义的 bootstrap :
https://github.com/yiisoft/yii2/blob/2.0.3/framework/web/Application.php#L60
不需要自己定义这个别名,你应该简单地使用另一个:
'aliases' => [
// Set the editor language dir
'@editor_lang_dir' => '@app/web/scripts/sceditor/languages/',
],
Yii2基础应用
要在配置文件中设置,请将其写入 $config 数组
'aliases' => [
'@name1' => 'path/to/path1',
'@name2' => 'path/to/path2',
],
参考:http://www.yiiframework.com/doc-2.0/guide-structure-applications.html
但如前所述here、
@yii 别名是在您将 Yii.php 文件包含在您的入口脚本中时定义的。其余别名在应用应用程序配置时在应用程序构造函数中定义。
如果你需要使用预定义的别名,写一个组件并link它在配置bootstrap数组
namespace app\components;
use Yii;
use yii\base\Component;
class Aliases extends Component
{
public function init()
{
Yii::setAlias('@editor_lang_dir', Yii::getAlias('@webroot').'/scripts/sceditor/languages/');
}
}
在配置文件中,将 'app\components\Aliases' 添加到 bootstrap 数组
'bootstrap' => [
'log',
'app\components\Aliases',
],
改进@vitalik_74的回答
你可以将它放在 config/web.php 中(如果你使用的是基本的 yii 应用程序,我不确定高级版本中的主要配置文件,但相同适用,只需将要求放在主配置文件中),以便它缩短为:
require(__DIR__ . '/aliases.php');