2013-02-03 84 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' 
      ) 
     ), 
    ) 
);