我的Yii2应用程序中有两个登录系统。第一个是使用User
表的默认登录系统,第二个是使用sms_account
表。在自定义控制器中,我已创建了登录操作actionLogin()
。我已经为自定义控制器添加了访问控制功能,但我遇到的问题是,当某人未登录时,会重定向到site/login
。我想在Yii2访问控制中将重定向更改为custom-controller/login
URL。我的代码是:
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login'],
'allow' => false,
'roles' => ['@']
],
[
'actions' => ['home'],
'allow' => true,
'roles' => ['@']
]
]
]
];
}
有谁能告诉我如何更改访问控制网址?
答案 0 :(得分:6)
您只需配置用户组件:
'user' => [
// ...
'loginUrl' => ['custom-controller/login'],
],
它应该是:
[
'actions' => ['login'],
'allow' => true,
'roles' => ['?']
],
答案 1 :(得分:2)
This is my updated answer . I thinks it's help you
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index','logout','client-create'], // your own action which permission the login
'rules' => [
[
'actions' => ['index','logout','client-create'], // your own action which permission the login
'allow' => true,
'roles' => ['@'],
],
],
'denyCallback' => function($rule, $action) {
Yii::$app->response->redirect(['login/login']);
},
],
];
}
答案 2 :(得分:-2)
试试这段代码。 注意:角色
[
'actions' => ['home'],
'allow' => true,
'roles' => ['?']
'matchCallback' => function ($rule, $action) {
return $this->redirect('index.php?r=controller/action');
}
]
答案 3 :(得分:-2)
您应该在your-project/vendor/yiisoft/yii2/web/User.php
第94行更改
public $loginUrl = ['custom-controller/custom-action'];
并且您的行为代码在下面给出
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index','logout'], // allow your own action in controller
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
];
}