Yii2 Custom http exceptions views
Solution 1
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,
]);
}
...
Solution 2
If you take a look here https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/controllers/SiteController.php
You can see that it uses an external action to handle the errors
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
You can either create your own ErrorAction file that extends the default one and use yours instead of the default one from Yii, or just comment out that action and create a normal actionError and put it in there.
SaidbakR
Listen to your heart and smile This is a short success story. My LinkedIn Profile! | My GitHub Repos. Just a Developer! Free Secure your Windows Computer against Viruses with Avast Anti Virus ##My favorite Answer:## Search for value in multidimensional array and get parent array in PHP If you find some useful answers or questions, please +1 vote! (x2 + y2 − 1)3 − x2y3 = 0 Chimply: Generate loading image indicators, buttons and badges. ###Main Tags### php javascript jquery cakephp sql mysql array ▒▒sємsєм▒▒ ▒▒ ▒▒ ▒▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒ القرآن الكريم Important Notice: Chimply.com is not my own website, but it is a website, I just like it!
Updated on July 16, 2022Comments
-
SaidbakR almost 2 years
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
andForbiddenHttpException
. 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? -
SaidbakR about 9 yearsThank you very much your answer inspired me to get my answer.