How to fill AccessControl in Yii 2?

In General, you need to do so:

1) Not an authorized user could only log in and all.
2) Authorized could

And if not difficult to explain, not what you can understand, like easy in in kind and not)) I would be grateful.

P. S: made this way, but if in different controllers action will have the same name?

namespace backend\controllers;

use yii\web\Controller;
use yii\filters\AccessControl;

class AppController extends Controller
{
 public function behaviors()
{
 return [
 'access' => [
 'class' => AccessControl::className(),
 'only' => ['login', 'logout', 'signup'],
 'rules' => [
[
 'allow' => true,
 'actions' => ['auth'],
 'roles' => ['?'],
],
[
 'allow' => true,
 'actions' => ['*'],
 'roles' => ['@'],
],
],
],
];
}
}
April 4th 20 at 00:41
1 answer
April 4th 20 at 00:43
Solution
You did everything correctly, however, when the user is a guest user filters yii2 automatically redirects him to the login page.

The default logon address: site/login. The address can be changed in the component user application. The default class yii\web\User to change the way to login, you must configure the user component in the config.

As you have the advanced template and have two applications (backend and frontend), then there are two ways of settings:

1. If the login form is a common
//common/config/main.php
'user' => [
 'loginUrl' => ['/admin/auth/login']
],

2. If the login form is different:
//backend/config/main.php
'user' => [
 'loginUrl' => ['/admin/auth/login']
],

//frontend/layout/main.php
'user' => [
 'loginUrl' => ['/auth/login']
],

You can also call the form from a template via js
//config
'user' => [
 'loginUrl' => ['/?login=true']
],

//frontend/layout/main.php
$script = <<< JS
$('#login').trigger('click'); //call the window with the login form
JS;

There are at least three methods for bridging page General filtering rules:

1. Put General rules in the"main" controllerfrom which inherit all controllers of the application.
2. Attach the global filter with the General rules that will block all action in the application.
3. Make common rules for separate classes of filtersthat can be connected to the controller module or the entire application.

I recommend to use the third option. I do not recommend to use the first option. In some cases the second.

The first option is worse his inheritance and redundancy. In the controller appears, the business logic, complicated testing.

If we develop an application in which not only has a web controller, but the api controller, console controller, then this approach we need to create three controller to the same General rules or apply another layer of inheritance. Finally, duplication of code or from a large nesting code does not go away.

If we leave three common controller, then when you change the rules, we can forget to change them elsewhere. Why something will work is not scheduled.

The second option gives our code a certain "mystique" (magic), and also complicates testing.

When using option 3 of the code being edited is always in single file on all controller modules and applications. Code is tested separately from controller and corresponds to the SOLID.

On this subject you can watch Dmitry Eliseev.

Find more questions by tags Yii