简体   繁体   中英

Yii alias not working in my main.php

I'm trying to create a site alias in my frontend/config/main.php but it doesn't seem to import all the classes from my common/components/

here is my file structure

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

this is what i have so far

$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.*',
    ),
         .......
);

everything using the site alias isn't loaded here. When i call Globals::isAdmin(); in my footer.php, i get an error.

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

my $root is set too

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

my Global class looks like this

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;
        }
}

Any idea what i'm missing or doing wrong? Thanks

May be your param merging has some problem because as you set $root and again in your require /frontend/config/params.php

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

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

figured it out. i had duplicate global classes in my common.components. and frontend.components. folders. silly me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM