【问题标题】:Yii2 Custom http exceptions viewsYii2 自定义 http 异常视图
【发布时间】:2015-02-04 22:01:56
【问题描述】:

在应用程序登录中,我有以下代码在记录错误时抛出 ...HttpException

// common/models/LoginForm.php which is called from the backend SiteController actionLogin method as $model = new LoginForm();

public function loginAdmin()
    {
      //die($this->getUser()->getRoleValue()."hhh");
      if ($this->getUser()->getRoleValue() >= ValueHelpers::getRoleValue('Admin') && $this->getUser()->getStatusValue() == ValueHelpers::getStatusValue('Active')){
        if ($this->validate()){
          return \Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30:0);         
        }
        else{
          throw new \yii\web\NotFoundHttpException('Incorrect Password or Username.');

        }       
      }
      else{
        throw new \yii\web\ForbiddenHttpException('Insufficient privileges to access this area.');
      }
    }

它工作正常,但我想自定义使用NotFoundHttpExceptionForbiddenHttpException 呈现的页面。我试图搜索Yii2 api 以查找任何可能在对象构造中定义视图的参数,但我找不到。那么,有没有办法自定义异常的视图呢?

【问题讨论】:

    标签: php yii2 yii2-advanced-app


    【解决方案1】:

    来自Mihai P.(谢谢)的回答,我得到了这个答案。我在vendor\yiisoft\yii2\web\ErrorAction.php 打开了错误类的文件,发现它有一个公共属性供查看,所以我决定使用它,因此我在actions 方法的error 数组中将它定义为如下:

    public function actions()
        {
            return [
                'error' => [
                    'class' => 'yii\web\ErrorAction',
                    'view' => '@common/views/error.php',
                ],
            ];
        }
    

    最后,在common 文件夹中,我必须创建一个名为views 的新文件夹,并使用以下简单代码填充一个名为error.php 的视图文件

    <?php
    $this->title = $name;
    echo $name;
    echo "<br>";
    echo $message;
    echo "<br>";
    echo $exception;
    

    视图$name, $message and $exception 中的三个变量由ErrorAction 对象提供,它们可以在last lines of that file 中找到

    ...
    else {
                return $this->controller->render($this->view ?: $this->id, [
                    'name' => $name,
                    'message' => $message,
                    'exception' => $exception,
                ]);
            }
    ...
    

    【讨论】:

      【解决方案2】:

      如果你看这里https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/controllers/SiteController.php

      您可以看到它使用外部操作来处理错误

      /**
           * @inheritdoc
           */
          public function actions()
          {
              return [
                  'error' => [
                      'class' => 'yii\web\ErrorAction',
                  ],
                  'captcha' => [
                      'class' => 'yii\captcha\CaptchaAction',
                      'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                  ],
              ];
          }
      

      你可以创建你自己的 ErrorAction 文件来扩展默认的并使用你自己的而不是 Yii 的默认的,或者只是注释掉那个动作并创建一个普通的 actionError 并把它放在那里。

      【讨论】:

      • 非常感谢您的回答启发了我得到答案。
      猜你喜欢
      • 2017-09-03
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多