首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数以查看扩展类中的操作。

参数以查看扩展类中的操作。
EN

Stack Overflow用户
提问于 2017-08-05 13:23:36
回答 2查看 363关注 0票数 0

我扩展了"dektrium/yii2-user“控制器的类如下,现在我希望在父类的呈现视图文件中有$authItems,我应该如何传递这个变量?

代码语言:javascript
复制
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是主要的类方法

代码语言:javascript
复制
 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,
        ]);
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-05 17:04:27

解决方案可以是不要调用父级::actionRegister ();并将代码直接添加到actionRegister中,最后将autItems添加到呈现函数数组参数中

代码语言:javascript
复制
  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
EN

Stack Overflow用户

发布于 2017-08-05 17:45:36

我会这样做:

向扩展类添加一个成员变量,如:

代码语言:javascript
复制
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();


    }
}

然后在父类中进行测试,以查看是否设置了该变量,例如:

代码语言:javascript
复制
 public function actionRegister()
{

    //.........

    return $this->render('register', [
        'model'  => $model,
        'module' => $this->module,
        'authitems' => (isset($this->authitems)) ? $this->authitems : null;
    ]);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45522418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档