Yii2 自定义 http 异常视图
Yii2 Custom http exceptions views
在应用程序登录中,我有以下代码在记录错误时抛出 ...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
和 ForbiddenHttpException
呈现的页面。我试图搜索 Yii2 api 以查找可能在对象构造中定义视图的任何参数,但我找不到。那么,有什么方法可以自定义异常的view吗?
如果你看这里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 并将其放入其中。
来自 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
,我发现它有一个 public 属性 供查看,所以,我决定使用它,因此我在 [=14= 中定义了它] actions
方法的数组如下:
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,
]);
}
...
在应用程序登录中,我有以下代码在记录错误时抛出 ...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
和 ForbiddenHttpException
呈现的页面。我试图搜索 Yii2 api 以查找可能在对象构造中定义视图的任何参数,但我找不到。那么,有什么方法可以自定义异常的view吗?
如果你看这里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 并将其放入其中。
来自 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
,我发现它有一个 public 属性 供查看,所以,我决定使用它,因此我在 [=14= 中定义了它] actions
方法的数组如下:
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,
]);
}
...