传递参数以查看来自扩展 class 的操作

Pass parameter to view action from extended class

我扩展了 "dektrium/yii2-user" 控制器的 class,现在我想在父 class 的渲染视图文件中有 $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();


    }
}

它是主要的class方法

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

一个解决方案可能是 不要调用 parent::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;
            ]);
      }
  }

我会这样做:

为你的扩展class添加一个成员变量,如:

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


    }
}

然后在你的父 class 中做一个测试,看看是否设置了那个变量,例如:

 public function actionRegister()
{

    //.........

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