2017-08-05 57 views
0

我擴展了「dektrium/yii2-user」控制器的類如下,現在我想讓$authItems在父類的渲染視圖文件中,我應該如何傳遞這個變量?傳遞參數以查看擴展類的動作

namespace app\controllers; 
use app\models\AuthItem; 
use dektrium\user\controllers\RegistrationController as BaseRegistrationController; 
class RegistrationController extends BaseRegistrationController 
{ 

    public function actionRegister() 
    { 
     $authItems = AuthItem::find()->all(); 

     return parent::actionRegister(); 


    } 
} 

其是主類方法

public function actionRegister() 
    { 
     if (!$this->module->enableRegistration) { 
      throw new NotFoundHttpException(); 
     } 

     /** @var RegistrationForm $model */ 
     $model = \Yii::createObject(RegistrationForm::className()); 
     $event = $this->getFormEvent($model); 

     $this->trigger(self::EVENT_BEFORE_REGISTER, $event); 

     $this->performAjaxValidation($model); 

     if ($model->load(\Yii::$app->request->post()) && $model->register()) { 
      $this->trigger(self::EVENT_AFTER_REGISTER, $event); 

      return $this->render('/message', [ 
       'title' => \Yii::t('user', 'Your account has been created'), 
       'module' => $this->module, 
      ]); 
     } 

     return $this->render('register', [ 
      'model' => $model, 
      'module' => $this->module, 
     ]); 
    } 

回答

0

的溶液可以是 不調用父:: actionRegister(); 並直接在您的actionRegister 添加代碼和持續的autItems添加到渲染功能參數數組

class RegistrationController extends BaseRegistrationController 
    { 
     public function actionRegister() 
     { 
      $authItems = AuthItem::find()->all(); 

      // return parent::actionRegister(); 

      if (!$this->module->enableRegistration) { 
       throw new NotFoundHttpException(); 
      } 

      /** @var RegistrationForm $model */ 
      $model = \Yii::createObject(RegistrationForm::className()); 
      $event = $this->getFormEvent($model); 

      $this->trigger(self::EVENT_BEFORE_REGISTER, $event); 

      $this->performAjaxValidation($model); 

      if ($model->load(\Yii::$app->request->post()) && $model->register()) { 
       $this->trigger(self::EVENT_AFTER_REGISTER, $event); 

       return $this->render('/message', [ 
        'title' => \Yii::t('user', 'Your account has been created'), 
        'module' => $this->module, 
       ]); 
      } 

      return $this->render('register', [ 
       'model' => $model, 
       'module' => $this->module, 
       'authItems' => $authItems; 
      ]); 
     } 
    } 
+0

在呈現方法中什麼是視圖路徑? 它傾斜recogonize視圖我改變到 ' 返回$這 - >渲染( '用戶/註冊/註冊',[ '模式'=> $模型, '模塊'=> $這個 - >模塊, ' authItems'=> $ authItems, ]); ' 但不工作 – moh

+0

你的註冊視圖在哪裏? ..你在哪裏放置你的註冊視圖?在哪個目錄中? – scaisEdge

+0

@scaisEdhe它是在視圖/用戶/註冊/目錄+視圖中web.php配置文件被作爲overrided ' '視圖'=> [ '主題'=> [ '路徑映射'=> [ ' @ dektrium/user/views'=>'@ app/views/user' ], ], ],' – moh

0

我會做這樣的事情:

一個成員變量添加到您的擴展類,例如如:

namespace app\controllers; 
use app\models\AuthItem; 
use dektrium\user\controllers\RegistrationController as BaseRegistrationController; 
class RegistrationController extends BaseRegistrationController 
{ 

    public $authitems; 

    public function actionRegister() 
    { 
     $this->authitems = AuthItem::find()->all(); 

     return parent::actionRegister(); 


    } 
} 

然後在你的父類,做一個測試,看看是否能變量設置,如:

public function actionRegister() 
{ 

    //......... 

    return $this->render('register', [ 
     'model' => $model, 
     'module' => $this->module, 
     'authitems' => (isset($this->authitems)) ? $this->authitems : null; 
    ]); 
}