How to force guest to login before view anything

Hi, i’m newbie to yii2 framework and i want to know how to force guest to login page before clicking any controller by using behaviour or something else which is efficient without adding actionBeforeAction to every single controller. Thanks before.

Derive all controllers from a base controller and in that one use this:

http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

thanks for the reply. I have tried solution from http://www.yiiframework.com/forum/index.php/topic/54255-newbies-question-to-yii2-how-can-i-force-user-to-login/

it is successful when adding this code to every controller in behaviour




     'access' => [

                'class' => AccessControl::className(),

                'rules' => [

                    [

                        'actions' => ['login', 'error'],

                        'allow' => true,

                    ],

                    [

                        'actions' => ['logout', 'index'], // add all actions to take guest to login page

                        'allow' => true,

                        'roles' => ['@'],

                    ],

                ],

            ],



is there a solution not to add to every controller?

I already figure it out.

in file config\web.php just put the code below




'components' => [ ... ],

'as access' => [

        'class' => \yii\filters\AccessControl::className(),//AccessControl::className(),

        'rules' => [

            [

                'actions' => ['login', 'error'],

                'allow' => true,

            ],

            [

                'actions' => ['logout', 'index'], // add all actions to take guest to login page

                'allow' => true,

                'roles' => ['@'],

            ],

        ],

    ],

'params' => $params,