Search code examples
yii2yii2-advanced-appyii2-basic-app

Pass parameter to view action from extended class


I extended "dektrium/yii2-user" controller's class as below, now I want to have $authItems in render view file of parent class, how should I pass this variable?

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();


    }
}

its is main class method

 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,
        ]);
    }

Solution

  • A solution could be don't invoke the parent::actionRegister(); and add the code directly in your actionRegister and last add the autItems to the render function array parameters

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