drmqzb5063 2015-02-04 22:01
浏览 88
已采纳

Yii2自定义http例外视图

In the application login I have the following code that throw ...HttpException on logging errors:

// 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.');
      }
    }

It is working fine, but I want to customize the page the rendered with each of NotFoundHttpException and ForbiddenHttpException. I tried to search the Yii2 api to find any parameters that may define view in the construct of the object but I could not find that. So, is there any way to custom the view of the exception?

  • 写回答

2条回答 默认 最新

  • dongzhuner6981 2015-02-05 00:15
    关注

    From Mihai P. (Thank you) answer, I have got this answer. I opened the file of the error class at vendor\yiisoft\yii2\web\ErrorAction.php and I have found that it has a public property for view, so, I decided to use it and hence I defined it in the error array of the actions method as follows:

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

    Finally, in common folder I had to create a new folder named views and fill it with a view file called error.php with the following simple code

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

    The three variables in the view $name, $message and $exception are supplied from the ErrorAction object and they could be found in the last lines of that file

    ...
    else {
                return $this->controller->render($this->view ?: $this->id, [
                    'name' => $name,
                    'message' => $message,
                    'exception' => $exception,
                ]);
            }
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败