Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2自定义http异常视图_Php_Yii2_Yii2 Advanced App - Fatal编程技术网

Php Yii2自定义http异常视图

Php Yii2自定义http异常视图,php,yii2,yii2-advanced-app,Php,Yii2,Yii2 Advanced App,在应用程序登录中,我有以下代码在记录错误时抛出…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

在应用程序登录中,我有以下代码在记录错误时抛出
…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.');
      }
    }
它工作正常,但我想使用
NotFoundHttpException
禁止HttpException
中的每一个自定义呈现的页面。我试图搜索,以查找任何可能定义对象构造中视图的参数,但找不到。那么,有什么方法可以自定义异常视图吗?

如果您看一下这里

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

/**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
您可以创建自己的ErrorAction文件,扩展默认文件并使用您的文件而不是Yii的默认文件,或者只是注释掉该操作并创建一个普通的actionError并将其放在那里。

来自(谢谢)答案,我得到了这个答案。我在打开了错误类的文件,发现它有一个供查看的公共属性,因此,我决定使用它,因此我在
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;

非常感谢你的回答激励我得到了答案。
...
else {
            return $this->controller->render($this->view ?: $this->id, [
                'name' => $name,
                'message' => $message,
                'exception' => $exception,
            ]);
        }
...