Yii 框架:setPathOfAlias() 返回 null [英] Yii Framework: setPathOfAlias() returns null
问题描述
我正在尝试将 MaxMind 的 GeoIp2 放入我的Yii 应用程序.基本上,我复制了src"下的文件.(请参阅上一个链接)在 protected -> 下供应商->最大思维
.我的应用下的文件夹结构如下:
I am trying to put MaxMind's GeoIp2 into my Yii application. Basically, I copied the files under "src" (see previous link) under protected -> vendors -> maxmind
. The folder structure under my application is the following:
protected
|---- vendors
|---- Zend
|---- maxmind
|---- Database
|---- Reader.php
|---- Model
|---- ...
|---- ...
之后,我在我的 index.php 文件中创建了路径别名:
After that, I created the path aliases into my index.php file:
Yii::setPathOfAlias('Zend', Yii::getPathOfAlias('application.vendors.Zend'));
Yii::setPathOfAlias('GeoIp2', Yii::getPathOfAlias('application.vendors.maxmind'));
该路径对于 'Zend' 别名工作正常,但对于 'GeoIp2' 则返回 null
失败.
The path works just fine for the 'Zend' alias, but it fails for 'GeoIp2' by returning null
.
Yii::createApplication("FrontendApplication", $config)->run();
echo "Path 1: " . Yii::getPathOfAlias("Zend"). '<br />'; // Correct path!
echo "Path 2: " . Yii::getPathOfAlias("GeoIp2"). '<br />'; // <==== NULL
echo "Maxmind path: " . Yii::getPathOfAlias('application.vendors.maxmind'). '<br />'; // correct path
var_dump(is_dir(Yii::getPathOfAlias('application.vendors.maxmind'))); // true
知道为什么会发生这种情况吗?
Any ideas why this could happen?
谢谢!
推荐答案
getPathOfAlias()
创建路径别名的调用是在CApplication
的构造函数中创建的.但是在包含 main.php 的时候还没有调用构造函数.
getPathOfAlias()
call to create path aliases are created in the constructor of CApplication
. But the constructor wasn't called yet at the point when main.php is included.
要配置路径别名,请使用 main.php 中的 aliases 属性.像这样:
To configure path aliases use the aliases property in your main.php. Like this:
return array(
'aliases' => array(
'GeoIp2' => 'application.vendors.maxmind',
),
...
这篇关于Yii 框架:setPathOfAlias() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!