Answer the question
In order to leave comments, you need to log in
How to properly configure Access Control for guest?
I want to open access to the main (index.php) but at the same time block all other addresses.
Can it be done in yii2??
Thinking about if(Yii::app()->user->isGuest) $this->redirect(Yii::app()->getModule('user')->loginUrl); but this is just a last resort.
<?php
return [
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'name' => "Hello_World",
'modules' => [
'admin' => [
'class' => 'mdm\admin\Module',
],
],
'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
'site/*',
],
],
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'mdm\admin\models\User',
'loginUrl' => ['admin/user/login'],
'class' => 'yii\web\User'
],
'authManager' => [
'class' => 'yii\rbac\PhpManager',
],
'authManager' => [
'class' => 'yii\rbac\PhpManager',
'defaultRoles' => ['user', 'admin'],
'itemFile' => '@common/config/rbac/items.php',
'assignmentFile' => '@common/config/rbac/assignments.php',
'ruleFile' => '@common/config/rbac/rules.php'
],
'formatter' => [
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'dd-MM-yyyy',
'decimalSeparator' => ',',
'thousandSeparator' => ' ',
],
],
];
Answer the question
In order to leave comments, you need to log in
config in your hands and events to help:
'on beforeAction' => function($event){
Yii::$app->controller->attachBehavior('',[
'class' => \yii\filters\AccessControl::className(),
'except' => ['login','error','index'],
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
]);
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question