2013-02-03 85 views
3

如何覆盖模块的Yii登录URL模块?覆盖模块的Yii登录网址

这里是基本应用程序的主要配置:

return array(
      ......... 

    // application components 
    'components'=>array(
     'user'=>array(    
      // enable cookie-based authentication 
      'allowAutoLogin'=>true, 
      'loginUrl' => '/site/login', 
     ), 
      .......... 
    ); 

后来才知​​道有代理模块,我想这在该模块中的登录网址是不同的登录方法也不同。

class AgentModule extends CWebModule { 

    public function init() { 
     // this method is called when the module is being created 
     // you may place code here to customize the module or the application 
     // import the module-level models and components 
     $this->setImport(array(
      'agent.models.*', 
      'agent.components.*', 
     )); 

     $this->defaultController = 'default'; 
     $this->layoutPath = Yii::getPathOfAlias('agent.views.layout'); 
     $this->components = array(
      'user' => array(
       'class' => 'AgentUserIdentity', 
       'loginUrl' => '/agent/default/login', 
      ) 
     ); 
    } 
      ....... 

但我不知道为什么这不起作用。 请帮忙...(T.T)

+0

您要使用不同的用户应用程序组件的模块和应用程序?或者他们是一样的? – dInGd0nG

+0

我想为模块使用不同的用户应用程序,例如:如果我在代理模块中,使用数据来自代理表的代理验证方法。 – GusDeCooL

回答

1

在您的问题的第二个代码块中,您为模块AgentModule定义了user组件。

如果我理解正确,在您的视图或控制器的某处使用Yii::app()->user->loginUrl,对不对?要获取在AgentModule中定义的网址,您应该使用Yii::app()->user->controller->module->user,但只有,而在AgentModule上下文中。

使它变得不同的最简单方法是为模块和应用程序定义某处登录网址,并使用此预定义的网址。

尝试阅读模块精铸件在这里:http://www.yiiframework.com/wiki/27/how-to-access-a-component-of-a-module-from-within-the-module-itself/

+0

是的,你解释的是正确的,就像我想要的。很快会阅读您给出的链接并提供反馈:) – GusDeCooL

2

使用此代码

 


class AgentModule extends CWebModule { 
    public $assetsUrl; 
    public $defaultController = 'Login'; 

    public function init() { 

     // this method is called when the module is being created 
     $this->setComponents(array(
       'errorHandler' => array(
         'errorAction' => 'admin/login/error'), 
       'user' => array(
         'class' => 'CWebUser', 
         'loginUrl' => Yii::app()->createUrl('admin/login'), 
       ) 
       ) 
     ); 

     Yii::app()->user->setStateKeyPrefix('_admin'); 

     // import the module-level models and components 
     $this->setImport(array(
       'admin.models.*', 
       'admin.components.*', 
       ) 
     ); 
    } 

    public function beforeControllerAction($controller, $action) { 

     if(parent::beforeControllerAction($controller, $action)) { 
      // this method is called before any module controller 
      //action is performed 
      $route = $controller->id . '/' . $action->id; 


      $publicPages = array(
        'login/login', 
        'login/error', 
      ); 

      if (Yii::app()->user->name !== 'admin' && !in_array($route, 
           $publicPages)) { 
       Yii::app()->getModule('admin')->user->loginRequired(); 
      } else { 
       return true; 
      } 
     } 
     else 
      return false; 
    } 
} 
0
 
class WebUser extends CWebUser { 

    public $module = array(); 

    public function init() { 
     parent::init(); 
     if(isset($this->module['loginUrl'])){ 
      if(!isset($this->module['moduleId'])){ 
       throw new Exception('moduleId Must defined'); 
      }else{ 
       $moduleId = Yii::app()->controller->module->id; 
       if($moduleId == $this->module['moduleId']){ 
        #$this->loginUrl = Yii::app()->request->redirect(Yii::app()->createUrl($this->module['loginUrl'])); 
        $this->loginUrl = array($this->module['loginUrl']); 
       } 
      } 
     } 

    } 

} 
 
after that you need to do some changes under config file i.e 
return array(
    'components' => array(
     'user' => array(
      'allowAutoLogin' => true, 
      'class' => 'WebUser', 
      'module' => array(
       'loginUrl' => '/r_admin', 
       'moduleId' => 'r_admin' 
      ) 
     ), 
    ) 
);