我正在尝试设置一个alias,Yii2但我得到的Invalid Parameter / Invalid path alias是下面的代码放在应用程序配置文件中:
'aliases' => [
    // Set the editor language dir
    '@editor_lang_dir' => '@webroot/scripts/sceditor/languages/',       
],
如果我删除@它的工作原理.
我注意到你可以这样做:
Yii::setAlias('@foobar', '@foo/bar');
...但我更愿意在app配置文件中设置它.这不可能吗?如果是这样,怎么样?
abd*_*ood 12
Yii2基本应用
要设置内部配置文件,请在$ config数组中写入
'aliases' => [
        '@name1' => 'path/to/path1',
        '@name2' => 'path/to/path2',
    ],
参考:http://www.yiiframework.com/doc-2.0/guide-structure-applications.html
但正如这里提到的,
在条目脚本中包含Yii.php文件时,将定义@yii别名.应用应用程序配置时,应用程序构造函数中定义了其余别名.
如果需要使用预定义的别名,请编写一个组件并将其链接到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/');
    }
}
在内部配置文件中,将"app\components\Aliases"添加到bootstrap数组中
    'bootstrap' => [
        'log',        
        'app\components\Aliases',        
],
在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/
| 归档时间: | 
 | 
| 查看次数: | 22063 次 | 
| 最近记录: |