我无法让 Auth 组件在 CakePHP 1.2.6 应用程序中执行我想要的重定向。
我有一个出现在所有页面上的登录表单,我想让用户留在他登录的页面上。例如,如果他正在查看另一个用户的个人资料,我想在登录后将他保留在那里,而不是将他重定向到该$this->Auth->loginRedirect
操作。另外,关于我的应用程序的另一件事是,我没有“仅经过身份验证的访问”页面,每个人都可以访问每个页面,但是如果您已登录,您将获得其他功能。
我从阅读文档中了解到,我需要设置autoRedirect
为 false 才能执行 login() 函数中的代码:
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()
显示为空。
下面,我发布了与 Auth 组件相关的 AppController 的内容:
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('*');
}
}
我似乎无法理解我做错了什么。