7

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.

SaidbakR
  • 11,955
  • 16
  • 89
  • 173

2 Answers2

26

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

edrian
  • 4,377
  • 5
  • 26
  • 34
  • 3
    Correct. And if you want to specify a status just do `throw new \yii\web\HttpException($status);` – Blizz May 26 '15 at 05:09
  • @Blizz also `HttpException` may have a custom message property i.e `\yii\web\HttpException(601, 'Upper case character not found')` – SaidbakR Jul 06 '17 at 08:26
3

Put this code to call 404 error page.

throw new \yii\web\NotFoundHttpException("Your Error Message.");
Dharmesh Goswami
  • 1,155
  • 2
  • 14
  • 30