When loading any controller or module, check if the user is a guest, send it to yii.local/auth (to the login page)

Can there be some filters that occur during application initialization? How to check, implement?

    2 answers 2

    Create a component, for example Init . Components can define the init method, it will be called before every action of any controller. In it we define, if not a guest, then a redirect to the login page (or another URL you need)

     namespace app\components; use Yii; use yii\helpers\Url; class Init extends \yii\base\Component { public function init() { if (\Yii::$app->getUser()->isGuest && \Yii::$app->getRequest()->url !== Url::to(\Yii::$app->getUser()->loginUrl) ) { \Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->loginUrl); } parent::init(); } } 

    Next, the component must be added to the config

     'components' => [ 'Init '=>[ 'class'=>'app\components\Init ' ], //другие компоненты ] 

    Add component initialization right at boot time (also written in config)

     'bootstrap' => ['log','Init'], 

    PS If you are not going to use here events or behaviors, instead of \yii\base\Component you can use \yii\base\Object

    • Thank you very much! clear and accessible - Mr. Music
    • @ Alexey Shimansky Can you tell me? Created the Init.php file with the specified content in the components folder. In the config folder in the web.php file added as in the example to the config and in bootstrap. But it displays an error Unknown bootstrapping component ID: Init - n.osennij
    • @ Aleksey Shimanskiy i.imgur.com/KYpBBSb.png - n.osennij
    • @ n.osennij I don’t know. There has long been a similar bug with yii regarding gii, maybe with some components in bootstrap there is also a version. You can put the latest version to see. But I unfortunately can not tell. - Alexey Shimansky
    • one
      @ n.osennij just wanted to write about it ..... what happens when copying from SO for some reason, utf-8 is copied from BOM, that is, they are added to characters imperceptible to the eye) several times it was with JS scripts, I found in the console .... but where I can remember from where I can see - Alexey Shimansky

    Found a typo in the code from @ Alexey Shimansky, which needs to be added to the config.

    Here:

     Init '=>[ 'class'=>'app\components\Init ' 

    after Init extra space in both cases.

    After that it worked.

    • Welcome to ruSO! Instead of thanks, there is an up arrow button. You give an answer to a specific question and information not associated with it is better to exclude. If you refer to someone's answer, then indicate @ VasyaPupkin, otherwise it is not clear what code we are talking about. - 0xdb
    • And messages about typos are better to issue a comment. But, alas, they are not yet available to you for the reputation of 50+. - 0xdb
    • @ 0xdb Unavailable, but we can help: moderators have the ability to convert responses to comments and do so for those responses that are checked in the queue as "the answer is a comment." - AK