Answer the question
In order to leave comments, you need to log in
How to throw an exception that will be handled by ErrorHandler?
Here I have created a filter
class FilterBannedIp extends ActionFilter {
public function events() {
return [Controller::EVENT_BEFORE_ACTION => 'beforeAction'];
}
public function beforeAction($event) {
$bannedIp = BannedIp::findByIp(Yii::$app->request->getUserIP());
if ($bannedIp && $bannedIp->isBanned(Yii::$app->request->getUserIP())) {
throw new ForbiddenHttpException('Ваш IP заблокирован');
return false;
}
return true;
}
}
public function init() {
$this->attachBehavior('filterBannedIp', FilterBannedIp::className());
}
exception 'yii\web\ForbiddenHttpException' with message 'Ваш IP заблокирован' in C:\OpenServer\domains\work2\basic\filters\FilterBannedIp.php:22
Stack trace:
#0 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\ActionFilter.php(71): app\filters\FilterBannedIp->beforeAction(Object(yii\web\ErrorAction))
#1 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#2 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Component.php(538): call_user_func(Array, Object(yii\base\ActionEvent))
#3 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Controller.php(259): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#4 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\web\Controller.php(108): yii\base\Controller->beforeAction(Object(yii\web\ErrorAction))
#5 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Controller.php(149): yii\web\Controller->beforeAction(Object(yii\web\ErrorAction))
#6 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('error', Array)
#7 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\web\ErrorHandler.php(80): yii\base\Module->runAction('site/error')
#8 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\ErrorHandler.php(95): yii\web\ErrorHandler->renderException(Object(yii\web\ForbiddenHttpException))
#9 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\ForbiddenHttpException))
#10 {main}
Previous exception:
exception 'yii\web\ForbiddenHttpException' with message 'Ваш IP заблокирован' in C:\OpenServer\domains\work2\basic\filters\FilterBannedIp.php:22
Stack trace:
#0 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\ActionFilter.php(71): app\filters\FilterBannedIp->beforeAction(Object(yii\base\InlineAction))
#1 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#2 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Component.php(538): call_user_func(Array, Object(yii\base\ActionEvent))
#3 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Controller.php(259): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#4 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\web\Controller.php(108): yii\base\Controller->beforeAction(Object(yii\base\InlineAction))
#5 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Controller.php(149): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#6 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('index', Array)
#7 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\web\Application.php(83): yii\base\Module->runAction('site/index', Array)
#8 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#9 C:\OpenServer\domains\work2\basic\web\index.php(12): yii\base\Application->run()
#10 {main}
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