除了Yii 2中的登录行为方法外,还有什么方法可以重定向到页面?
我的行为方法内容:
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => AccessControl::className(),
'only' => [ 'create','update' ],
'rules' => [
[
'allow' => true,
'actions' => [ 'create'],
'roles' => ['@'],
],
[
'allow' => true,
'actions' => ['logout'],
'roles' => ['?'],
],
],
],
];
}
但是它重定向到登录。我需要指定另一个重定向页面或调用:
throw new \yii\web\HttpException(403, 'The requested Item could not be found.');
最佳答案
您需要更改loginUrl
类的yii\web\User
属性。
如果要全局更改,请编辑配置:
'components' => [
'user' => [
'loginUrl' => ['site/sign-in'],
],
],
如果您需要在特定的 Controller 或操作中进行更改,也可以这样设置:
Yii::$app->user->loginUrl = ['site/sign-in'];
您需要在 Controller 中重写
beforeAction()
方法。在这种情况下执行所有访问检查。/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
// If you want to change it only in one or few actions, add additional check
Yii::$app->user->loginUrl = ['site/sign-in'];
return true;
} else {
return false;
}
}
有关更多详细信息,请参阅有关property和event的官方文档。
关于yii2 - 重定向到除Yii 2行为登录外的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27408064/