In my Yii2 application, I am trying to force all users to authenticate. If they have not yet been authenticated, they should be redirected to the login page.
In Yii1, I did this by creating a class that would check if the user was logged in and onBeginRequest this class to the onBeginRequest behavior in my main configuration file.
// Yii 1 'behaviors' => array( 'onBeginRequest' => array( 'class' => 'application.components.RequireLogin', ) ),
How can I get the same behavior in Yii2? I know that I can use the behavior for this, but I do not want to add this behavior to my main configuration file, so all requests are first checked for authentication.
The working behavior method is as follows:
// Yii2 public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['login', 'error'], 'allow' => true, ], [ 'allow' => true, 'roles' => ['@'], ], ], ], ]; }
php yii rbac yii2
jagsler
source share