Yii别名不在我的main.php中工作

时间:2014-06-08 03:48:02

标签: php yii

我正在尝试在site中创建一个frontend/config/main.php别名,但它似乎无法导入common/components/

中的所有类

这是我的文件结构

backend/
    common/
        components/
            Globals.php
        config/
            params.php
            params-local.php
        lib/
            yii/
        migrations/
        models/
            Comment.php
            Extension.php
            ...
    console/
        commands/
            SitemapCommand.php
            ...
        config/
            main.php
            main-local.php
            params.php
            params-local.php
        runtime/
        yiic.php
    frontend/
        components/
        config/
            main.php
            main-local.php
            params.php
            params-local.php
        controllers/
            SiteController.php
            ...
        lib/
        models/ 
            ContactForm.php
            SearchForm.php      
        runtime/
        views/
            layouts/
            site/
        www/
            assets/
            css/
            js/
            index.php
    yiic
    yiic.bat

这是我到目前为止所拥有的

$root=dirname(__FILE__).'/../..';

//define a path alias, site
Yii::setPathOfAlias('site',$root);

//merging together our frontend params with our common application params
$params=array_merge(
    require($root.'/common/config/params.php'),
    require($root.'/frontend/config/params.php')
);

return array(
    'name'=>'site name',

    'basePath'=>$root.'/frontend',

    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>$params,

    // preloading 'app' component in frontend/extensions
    'preload'=>array('log','app'),

    // auto loading model and component classes from this application and our common application
    'import'=>array(
        'site.common.components.*',
        'site.common.models.*',
        'site.common.extensions.mailer.components.*',
        'application.components.*',
        'application.models.*',
    ),
         .......
);

此处未加载使用网站别名的所有内容。当我在footer.php中调用Globals::isAdmin();时,出现错误。

Fatal error: Call to undefined method Globals::isAdmin() in /Library/WebServer/Documents/dev/frontend/views/layouts/footer.php on line 81

我的$root也设置了

/Library/WebServer/Documents/dev/frontend/config/../..

我的Global类看起来像这样

class Globals {

         /* 
          * Check if user is an admin
          */ 
         public static function isAdmin() 
         {
          $testId = Yii::app()->user->id;
          $role = User::model()->findByPk($testId);

          return ($role['role'] == 1) ? true : false;
        }
}

知道我错过了什么或做错了什么?感谢

2 个答案:

答案 0 :(得分:0)

可能是你的param合并有一些问题,因为你在你的require /frontend/config/params.php中再次设置$ root

/Library/WebServer/Documents/dev/frontend/config/../..

$params=array_merge(
require($root.'/common/config/params.php'),
require($root.'/frontend/config/params.php')
);

答案 1 :(得分:0)

想通了。我的common.components.frontend.components.个文件夹中有重复的全局类。傻了我