Setting aliases in Yii2 within the app config file(在应用配置文件中的 Yii2 中设置别名)
问题描述
我正在尝试在 Yii2 中设置一个 alias 但我得到了一个 Invalid Parameter/Invalid path alias 下面的代码放在应用程序配置文件中:
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?
推荐答案
在 config 文件夹中创建文件 aliases.php.并把这个:
In config folder create file aliases.php. And put this:
Yii::setAlias('webroot', dirname(dirname(__DIR__)) . '/web');
Yii::setAlias('editor_lang_dir', '@webroot/scripts/sceditor/languages/');
在 index.php 文件中的 web 文件夹中:require(__DIR__ .'/../config/aliases.php');
In web folder in index.php file put:
require(__DIR__ . '/../config/aliases.php');
之前:
(new yiiwebApplication($config))->run();
如果在视图文件中运行echo:
If run echo in view file:
echo Yii::getAlias('@editor_lang_dir');
显示如下:
C:OpenServerdomainsyii2_basic/web/scripts/sceditor/languages/
这篇关于在应用配置文件中的 Yii2 中设置别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在应用配置文件中的 Yii2 中设置别名
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
