Yii2 enforce 404 or 500 error

SaidbakR picture SaidbakR · May 26, 2015 · Viewed 19.4k times · Source

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.

Answer

edrian picture edrian · May 26, 2015

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