Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

php - Setting aliases in Yii2 within the app config file

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yii2 basic application

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

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

Ref: 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.

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

namespace appcomponents;


use Yii;
use yiiaseComponent;


class Aliases extends Component
{
    public function init() 
    {
       Yii::setAlias('@editor_lang_dir', Yii::getAlias('@webroot').'/scripts/sceditor/languages/');
    }
}

and inside config file, add 'appcomponentsAliases' to bootstrap array

    'bootstrap' => [
        'log',        
        'appcomponentsAliases',        
],

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...