Answer the question
In order to leave comments, you need to log in
How can I populate an AccessControl in yii2?
In general, you need to do this:
1) An unauthorized user could only log in and that's it.
2) An authorized person could do everything
And if it’s not difficult with explanations, I can’t figure something out, it seems easy in kind and not)) I would be grateful.
PS: That's how I did it, but what if the action has the same names in different controllers?
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' => ['@'],
],
],
],
];
}
}
Answer the question
In order to leave comments, you need to log in
You did everything right, however, when the user is a guest - yii2 filters will automatically redirect him to the login page.
Default login address: site/login
. The address can be changed in the user component of the application. The class is used by default. yii\web\User
To change the path to the login, you must configure the user component in config.
Since you have an advanced template and two applications (backend and frontend), there are two ways to configure:
1. If the login form is shared
//common/config/main.php
'user' => [
'loginUrl' => ['/admin/auth/login']
],
//backend/config/main.php
'user' => [
'loginUrl' => ['/admin/auth/login']
],
//frontend/layout/main.php
'user' => [
'loginUrl' => ['/auth/login']
],
//config
'user' => [
'loginUrl' => ['/?login=true']
],
//frontend/layout/main.php
$script = <<< JS
$('#login').trigger('click'); //вызываем окно с формой логина
JS;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question