P
P
Pavel Bykov2019-10-04 22:58:10
Yii
Pavel Bykov, 2019-10-04 22:58:10

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');
    }
}

API Controller =>
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'
            ]
        ];
    }
}

All controller files are located in the controllers folder.
PS Disabled the errorHandler property in the web.php configuration

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question