php 在应用配置文件中的 Yii2 中设置别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28797051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:04:23  来源:igfitidea点击:

Setting aliases in Yii2 within the app config file

phpyiiyii2

提问by Brett

I'm trying to set an aliasin Yii2but I'm getting a Invalid Parameter / Invalid path aliasfor the below code that is placed in the app config file:

我正在尝试设置一个aliasinYii2但我得到了一个Invalid Parameter / Invalid path alias放置在应用程序配置文件中的以下代码:

'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?

...但我更愿意在应用程序配置文件中设置它。这不可能吗?如果是这样,如何?

采纳答案by vitalik_74

In configfolder create file aliases.php. And put this:

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

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

In webfolder in index.phpfile put: require(__DIR__ . '/../config/aliases.php');

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

Before:

前:

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

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

If run echoin view file:

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

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

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

Show like this:

像这样显示:

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

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

回答by abdulwadood

Yii2 basic application

Yii2 基础应用

To set inside config file, write this inside $config array

要在配置文件中设置,请将其写入 $config 数组中

'aliases' => [
        '@name1' => 'path/to/path1',
        '@name2' => 'path/to/path2',
    ],

Ref: http://www.yiiframework.com/doc-2.0/guide-structure-applications.html

参考:http: //www.yiiframework.com/doc-2.0/guide-structure-applications.html

But as mentioned here,

但正如这里提到的,

The @yii alias is defined when you include the Yii.php file in your entry script. The rest of the aliases are defined in the application constructor when applying the application configuration.

@yii 别名是在您的入口脚本中包含 Yii.php 文件时定义的。其余的别名是在应用应用程序配置时在应用程序构造函数中定义的。

If you need to use predefined alias, write one component and link it in config bootstrap array

如果您需要使用预定义的别名,请编写一个组件并将其链接到 config 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/');
    }
}

and inside config file, add 'app\components\Aliases' to bootstrap array

在配置文件中,将“app\components\Aliases”添加到引导数组

    'bootstrap' => [
        'log',        
        'app\components\Aliases',        
],

回答by soju

@webrootalias is not available at this point, it is defined during application bootstrap :

@webroot别名此时不可用,它是在应用程序引导期间定义的:

https://github.com/yiisoft/yii2/blob/2.0.3/framework/web/Application.php#L60

https://github.com/yiisoft/yii2/blob/2.0.3/framework/web/Application.php#L60

No need to define this alias yourself, you should simply use another one :

不需要自己定义这个别名,你应该简单地使用另一个:

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

回答by Corbee

To improve on @vitalik_74's answer

改进@vitalik_74 的回答

you can place it in config/web.php instead(if you are using the basic yii app, I'm not sure about the main config file in the advance version, but the same applies, just put the require on the main config file) so that it gets shorten to:

你可以把它放在 config/web.php 中(如果你使用的是基本的 yii 应用程序,我不确定高级版本中的主配置文件,但同样适用,只需将 require 放在主配置文件中) 使其缩短为:

require(__DIR__ . '/aliases.php');