Setting aliases in Yii2 within the app config file

后端 未结 4 767
孤独总比滥情好
孤独总比滥情好 2020-12-16 05:13

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

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 05:26

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

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

提交回复
热议问题