[英]CakePHP login method read session if exists for redirect
我禁用了autoRedirect
因此我可以在用戶控制器的登錄方法中做一些額外的爵士工作,並使用Session將其發送回它們的來源。
class UsersController extends AppController
{
var $name = 'Users';
var $components = array('Session');
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow(array('login','logout','admin_logout','admin_login'));
$this->Session->write('back_to', $this->referer());
}
/**
* Log in
*/
function admin_login ()
{
$this->set('title_for_layout', 'Log in – Admin —');
if(!(empty($this->data)) && $this->Auth->user())
{
$back_to = $this->Session->read('back_to');
if($back_to)
{
$this->redirect($back_to, null, true);
}
else
{
$this->redirect($this->Auth->redirect(), null, true);
}
}
if($this->Auth->user())
{
$this->redirect($this->Auth->redirect(), null, true);
}
}
因此,想法是,如果用戶擁有會話(99.%的時間),則在提交表單時它將把用戶發送到上一頁,如果沒有,則發送到默認的loginRedirect。
注意:通過將autoRedirect
設置為false,CakePHP將不再使用Auth.Redirect
會話! 因此,存儲在此處的值已不再被應用程序使用,這是故意的!
我遇到的問題是,由於上面代碼中“此人”注釋下面的功能,我的應用程序始終會將用戶發送到儀表板。 如果我刪除該功能,那么用戶將一直被返回到登錄表單,但他們將一直登錄!
有人可以幫忙嗎?
謝謝
更新:這是我的appcontroller代碼:
class AppController extends Controller
{
var $components = array('Auth','Session');
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->authorize = 'controller';
$this->Auth->autoRedirect = false;
$this->Auth->loginAction = array('controller'=>'users','action'=>'login','admin'=>true
);
$this->Auth->loginRedirect = array('admin'=>true,'controller' => 'dashboard', 'action' => 'index');
$this->Auth->logoutRedirect = array('admin'=>false,'controller' => 'pages', 'action' => 'display','home');
}
function isAuthorized()
{
return true;
}
}
重定向后您不存在。 嘗試將重定向簽名更改為:
$this->redirect( $back_to, null, true );
第二個參數是狀態碼,第三個參數是是否停止處理當前動作。 這應該可以防止您下降到我猜測是正在執行的最后一個重定向。
鑒於下面我們的長長的注釋“討論”,請嘗試調整admin_login()
方法,如下所示:
if(!(empty($this->data)) && $this->Auth->user()) {
$back_to = $this->Session->read('back_to');
if($back_to) {
$this->redirect($back_to, null, true);
}
else {
$this->redirect($this->Auth->redirect(), null, true);
}
}
else {
# Only write back_to when you arrive at the login page without data
$this->Session->write('back_to', $this->referer());
}
function login (){ if(!empty($this->data) && !$this->Auth->user()){ $this->Session->write('back_to', $this->referer()); }
刪除該Session-> write在beforeFilter中。 而且您不需要$this->Auth->allow(array('login','logout'));
if(!(empty($this->data)) && $this->Auth->user())
{
$back_to = $this->Session->read('back_to');
$auth_redirect = $this->Session->read('Auth.redirect');
if($auth_redirect)
{
$this->redirect($auth_redirect, null, true);
}
else if($back_to)
{
$this->redirect($back_to, null, true);
}
else
{
$this->redirect($this->Auth->redirect(), null, true);
}
}
else
{
$this->Session->write('back_to', $this->referer());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.