简体   繁体   中英

Yii2 - Restricting Pages by IP

I want to restrict the Error pages by IP in the development environment.

In the site controller, I tried to add the access rule, but that doesn't seems to work with error action like:

[
      'actions' => ['error'],
      'allow' => true,
      'roles' => ['?'],
      'ips' => ['my-ip'],
],

在此处输入图片说明

It is not recommended that you set the access permission of the error page. This will give another prompt message that does not have access to the error page when the user without permission has an error. The prompt is also not friendly.

If your goal is to display different levels of error pages for different ips, you can override the \\yii\\web\\ErrorAction to display different error pages for different privileged users, for example:

class MyErrorAction extends \yii\web\ErrorAction {
    public function run()
    {
        if ('ip1' === \Yii::$app->request->userIP) {
            return $this->renderHtml1();
        }

        if ('ip2' === \Yii::$app->request->userIP) {
            return $this->renderHtml2();
        }

        return $this->renderDefaultHtml();
    }
}

Then in the project's configuration file, configure the errorHandler's errorAction as your MyErrorAction, for example:

[
    'components' => [
        'errorHandler' => [
            'errorAction' => MyErrorAction::class
        ],
    ],
]

Or you can override the \\yii\\web\\ErrorHandle::renderException() method to control the content of the presentation and configure errorHandle in the configuration file as your MyErrorHandler

This answer is translated from Google Translate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM