Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 enforce 404 or 500 error

In my controller's action of Yii2 application suppose the following:

public function actionView($i)
{
  if ($i < 20)
  {
    //I want execute error 404
  }
}

All what I can to do now is just setting a flash message and redirect to another action. However, I want to generate 404 Page not found response.

like image 727
SaidbakR Avatar asked May 26 '15 03:05

SaidbakR


2 Answers

I think you can just throw a 404 not found exception:

throw new \yii\web\NotFoundHttpException();

Yii error manager will handle that predefined exception and show the appropiate error page.

You can configure / design your error pages editing views/site/error.php

You can read how can customize that page in the following link

like image 128
edrian Avatar answered Nov 07 '22 06:11

edrian


Put this code to call 404 error page.

throw new \yii\web\NotFoundHttpException("Your Error Message.");
like image 28
Dharmesh Goswami Avatar answered Nov 07 '22 05:11

Dharmesh Goswami