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?
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
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.osennijFound 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.
Source: https://ru.stackoverflow.com/questions/468788/
All Articles