cakePHP-登录后重定向
首先要说的是,我也检查了,但这似乎并不能解决问题 预期结果:成功登录后,重定向到“/admin/dashboard/index” 实际结果:成功登录后,应用程序将重定向到“/users/login”。登录正确,我可以在会话中看到它。此外,当在浏览器中手动导航到“/admin/dashboard”时,它会重定向到“/admin”,然后按照Config/routes.php中定义的规则进入无限重定向循环 以下是相关代码: 在Config/core.php中cakePHP-登录后重定向,php,cakephp,authentication,redirect,Php,Cakephp,Authentication,Redirect,首先要说的是,我也检查了,但这似乎并不能解决问题 预期结果:成功登录后,重定向到“/admin/dashboard/index” 实际结果:成功登录后,应用程序将重定向到“/users/login”。登录正确,我可以在会话中看到它。此外,当在浏览器中手动导航到“/admin/dashboard”时,它会重定向到“/admin”,然后按照Config/routes.php中定义的规则进入无限重定向循环 以下是相关代码: 在Config/core.php中 Configure::write('Rou
Configure::write('Routing.prefixes',数组('admin')代码>
在Config/routes.php中
Router::connect('/admin',数组('controller'=>'dashboard','action'=>'index','admin'=>true))代码>
这是我在UsersController中的登录操作
if ( $this->request->is( 'post' ) ) {
if ( $this->Auth->login() ) {
$this->redirect( $this->Auth->redirectUrl() );
} else {
$this->BASession->err( __( 'Login error. Please check your data!' ) );
}
}
if ( $this->request->is( 'post' ) ) {
if ( $this->Auth->login() ) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Your username or password was incorrect.'));
}
}
最后,这是我的身份验证配置
$this->Auth->loginAction = array( 'controller' => 'users', 'action' => 'login', 'admin' => false );
$this->Auth->logoutRedirect = array( 'controller' => 'users', 'action' => 'login', 'admin' => false );
$this->Auth->loginRedirect = array( 'controller' => 'dashboard', 'action' => 'index', 'admin' => true );
$this->Auth->autoRedirect = true;
相关地说,随着时间的推移而变化
$this->Auth->autoRedirect=false代码>
没有任何区别
存在一个带有空“admin_index”函数的DashboardController类,甚至是一个空视图
在登录函数中,调试“$this->Auth->redirectUrl()”显示“/admin”
PHP版本5.4.10-cakePHP版本2.5.1
我希望我已经提供了足够的信息,如果没有,请询问
谢谢大家! 在AppController.php中编写以下代码
public function beforeFilter() {
$this->Auth->loginAction = array( 'controller' => 'users', 'action' => 'login', 'admin' => false );
$this->Auth->logoutRedirect = array( 'controller' => 'users', 'action' => 'login', 'admin' => false );
$this->Auth->loginRedirect = array( 'controller' => 'dashboard', 'action' => 'index', 'admin' => true );
}
UsersController中的登录操作
if ( $this->request->is( 'post' ) ) {
if ( $this->Auth->login() ) {
$this->redirect( $this->Auth->redirectUrl() );
} else {
$this->BASession->err( __( 'Login error. Please check your data!' ) );
}
}
if ( $this->request->is( 'post' ) ) {
if ( $this->Auth->login() ) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Your username or password was incorrect.'));
}
}
尝试在AppController的组件变量中设置Auth参数,而不是将其设置为正确的Auth对象public$components=array('Auth'=>array('loginAction'=>array('controller'=>'users','action'=>login','plugin'=>false),'LoginDirect'=>array('controller'=>'dashboard'、'action'=>'index'、'prefix'=>'admin')、'authError'=>'your not pass'));
对不起,我对堆栈标记的格式不太熟悉。您是否为Auth组件配置了任何授权?嘿@ADmad您成功了!通过在内部的“isAuthorized”函数中设置“return true”,它现在可以工作了“DashboardController”。无论如何,这很奇怪,我认为问题与身份验证问题有关,而不是授权问题。但是……很好的回答!我仍然不明白它为什么会这样做,但我会深入研究。一个旁白问题……有没有办法告诉应用程序在授权失败时重定向到哪里?”$this->Auth->redirectUrl()“显示“/admin”
这是身份验证和路由正常工作的提示。因此,下一个逻辑问题是授权:)如果授权失败,将在会话中设置消息。使用DebugKit,您可以轻松检查会话值。另外,如果您通过身份验证$this->Auth->user()
将返回用户记录,而不考虑授权信息。顺便说一句,isAuthorized()
应该在AppController中,而不是特定的控制器中。您能解释一下您对OP提供的代码做了哪些更改,以及如何解决这个问题吗?首先,他无法解释他在AppController的beforeFilter中编写了一个代码,其次需要使用auth$this->auth->redirect()使用重定向。这就是问他代码在哪里,而不是做假设。另外,$this->redirect($this->Auth->redirectUrl());
是正确的代码。$this->Auth->redirect()
您使用的代码实际上已被弃用。