問題描述
我無法讓 Auth 組件在 CakePHP 1.2.6 應用程序中執行我想要的重定向.
I am having trouble getting the Auth component do the redirects I want in a CakePHP 1.2.6 app.
我有一個顯示在所有頁面上的登錄表單,我想將用戶保留在他登錄的頁面上.例如,如果他正在查看另一個用戶的個人資料,我想在登錄后將他留在那里,而不是將他重定向到 $this->Auth->loginRedirect
操作.此外,關于我的應用程序的另一件事是我沒有僅限經過身份驗證的訪問"頁面,每個人都可以訪問每個頁面,但如果您登錄,您可以獲得其他功能.
I have a login form that appears on all pages and I want to keep the user on the page he logs in on. For example, if he is viewing another user's profile, I want to keep him there after logging in, not redirect him to the $this->Auth->loginRedirect
action. Also, another thing about my app is that I have no "authenticated access only" pages, every page is accessible to everyone, but if you're logged in you get additional features.
我從閱讀文檔中了解到我需要設置autoRedirect
為 false 以獲取要執行的 login() 函數中的代碼:
What I understood from reading the documentation is that I need to set autoRedirect
to false to get the code in the login() function to be executed:
class UsersController extends AppController {
var $name = 'Users';
var $helpers = array('Html', 'Form','Text');
function beforeFilter() {
$this->Auth->autoRedirect = false;
}
function login() {
$this->redirect($this->referer());
}
function logout() {
$this->redirect($this->Auth->logout());
}
/* [...] */
}
這目前破壞了我的身份驗證.我注意到(從日志中)如果我在登錄功能中保留重定向并將 autoRedirect
設置為 false,則 $this->data
中的密碼字段login()
函數顯示為空.
This currently breaks my authentication. I've noticed (from the logs) that if I leave the redirect in the login function and set autoRedirect
to false, the password field in $this->data
in the login()
function appears as empty.
下面,我已經發布了與 Auth 組件相關的 AppController 的內容:
Below, I've posted the contents of AppController that relate to the Auth component:
public function beforeFilter() {
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'usercars', 'action' => 'homepage');
$this->allowAccess();
// build wishlist if the user is logged in
if ($currentUser = $this->Auth->user()) {
$wishlists = $this->buildWishlist($currentUser);
$this->set('wishlists', $wishlists);
}
}
private function allowAccess() {
if(in_array($this->name, /* all my controller names */)) {
$this->Auth->allow('*');
}
}
我似乎無法理解我做錯了什么.
I can't seem to understand what I'm doing wrong.
推薦答案
Add parent::beforeFilter();到用戶控制器中的 beforeFilter:
Add parent::beforeFilter(); to beforeFilter in the user controller:
function beforeFilter() {
$this->Auth->autoRedirect = false;
parent::beforeFilter();
}
您還可以將重定向替換為您的用戶控制器的登錄方法:
You can also replace the redirect with this to the login method of your user controller:
$this->redirect($this->Auth->redirect());
Auth->redirect() 返回用戶在被帶到登錄頁面或 Auth->loginRedirect 之前登陸的 url.
Auth->redirect() returns the url where the user landed before being taken to the login page or Auth->loginRedirect.
這篇關于CakePHP Auth 組件重定向問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!