S
S
Sergey Semenko2015-03-17 23:59:38
Yii
Sergey Semenko, 2015-03-17 23:59:38

Why doesn't ActionFilter render the correct file?

Here I created a filter, but it does not work, it just displays the main page (/site/index)

class FilterBannedIp extends ActionFilter {
    public function beforeAction($action) {
        $bannedIp = BannedIp::findByIp(Yii::$app->request->getUserIP());

        // die(var_dump(Yii::$app->request->getUserIP()));
        // die($bannedIp->ip);

        if ($bannedIp && $bannedIp->isBanned(Yii::$app->request->getUserIP())) {
            if (Yii::$app->request->isAjax) {
                return 'Error: Ваш IP заблокирован';
            } else {
                return $action->controller->render('/site/banned_ip', [
                    'bannedIp' => $bannedIp,
                ]);
            }
        }

        return parent::beforeAction($action);
    }
}

The condition is triggered, I checked, and if I specify a non-existent file in $action->controller->render I get an error, but whichever template I don't specify, it always displays the main page.
And how in general it is correct to make the filter on IP to all controllers?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Ozersky, 2015-03-18
@abler98

Because the task of the filter is to check whether there is access to the action or not.
It can return true or false, it's up to the controller to render the response.
Inside beforeAction, you can throw an exception that will catch errorHandler, but there in errorHandler->errorAction you can render an error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question