Search code examples
yii2yii2-basic-app

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.


Solution

  • 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