【问题标题】:Yii framework: importing classes while in the config/main.php fileYii 框架:在 config/main.php 文件中导入类
【发布时间】:2011-12-07 12:58:23
【问题描述】:

尝试在config/main.php 中导入一个类(返回 Yii 配置之前):

 //class LanguageSettings is placed directly in protected/components/LanguageSettings.php
 Yii::import('application.components.*');
 LanguageSettings::currentLanguage();

导致致命错误:

 Fatal error: Uncaught exception 'CException' with message 
 'Alias "application.components.*" is invalid. Make sure it points to an existing 
 directory or file.' in C:\xampp\htdocs\yii\framework\YiiBase.php:

为什么? Yii 可以在导入配置数组之前正确解析别名吗?

【问题讨论】:

    标签: class yii config alias import


    【解决方案1】:

    对不起,我认为它不会起作用,因为 basePath 尚未设置,因此 Yii 无法计算 application.components.* 所指的位置。

    【讨论】:

    • 感谢您的回答。有没有其他方法可以(手动)导入路径?
    • 好吧,因为通常 basePath(在 config/main.php 中)被定义为 dirname(FILE).DIRECTORY_SEPARATOR.'..' 你可以在变量中定义它( $myBasePath) 在 main.php 的顶部,然后手动执行 import_once($myBasePath . '/components/myComponent.php') 绕过 Yii 的自动导入功能。有点hacky,虽然......
    • 哈克但很有帮助。谢谢 !附言。 import_once = 包含/require_once.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2022-05-23
    • 1970-01-01
    • 2011-01-01
    相关资源
    最近更新 更多