Answer the question
In order to leave comments, you need to log in
How to display multiple types of 404 errors in Yii2?
Hello, I have 2 controllers in yii2, one works on the website, and the second on the REST API, and the question is how can I handle 404 differently so that on the API controller I can output a JSON string stating that the method was not found, but on the controller website 404 page? (actionError method doesn't help)
Web Controller =>
class AdminController extends \yii\web\Controller
{
public function beforeAction($action)
{
if(!(\Yii::$app->session->has('is_admin'))) {
$this->layout = null;
$this->action->actionMethod = 'actionEnterAdmin';
}
return parent::beforeAction($action);
}
/* Вот этот контроллер должен сработать если не найдена страница по идеи... */
public function actionError() {
$this->render('error');
}
}
class ApiController extends \yii\web\Controller
{
/* Вот этот контроллер должен сработать если не найдена страница по идеи... */
public function actionError() {
\Yii::$app->response->format = Response::FORMAT_JSON;
return [
'error' => [
'code' => 404,
'text' => 'method is not found'
]
];
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question