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?
In
config
folder create filealiases.php
. And put this:In
web
folder inindex.php
file put:require(__DIR__ . '/../config/aliases.php');
Before:
(new yii\web\Application($config))->run();
If run
echo
in view file:echo Yii::getAlias('@editor_lang_dir');
Show like this:
C:\OpenServer\domains\yii2_basic/web/scripts/sceditor/languages/
Yii2 basic application
To set inside config file, write this inside $config array
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
and inside config file, add 'app\components\Aliases' to bootstrap array
To improve on @vitalik_74's answer
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:
@webroot
alias is not available at this point, it is defined during application bootstrap :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 :