我已禁用autoRedirect
,所以我可以在我的用戶控制器的登錄方法中做一些額外的爵士樂,並使用會話將它們發回他們來自的地方。CakePHP登錄方法讀會話如果存在重定向
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
會話!所以存儲在那裏的值不再被應用程序使用,並且是有意的!
我遇到的問題是我的應用總是將用戶發送到儀表板,因爲上面的代碼中'This one'註釋下面的功能。如果我刪除了那個函數,那麼用戶只是一直髮送回登錄表單,但他們會被登錄!
任何人都可以幫忙嗎?
感謝
更新:這裏是我的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;
}
}
根據這一頁:http://book.cakephp.org/view/1270/loginRedirect你應該使用默認的重定向機制來做你想做的事情。除非你想要重定向,即使最後一次不需要登錄... –