CakePHP的验证组件重定向问题 [英] CakePHP Auth component redirect issue
问题描述
我遇到麻烦验证组件我想重定向在CakePHP的1.2.6应用程序。
I am having trouble getting the Auth component do the redirects I want in a CakePHP 1.2.6 app.
我有一个在所有页面上会出现一个登录表单,我想保持用户他登录在网页上。例如,如果他查看其他用户的个人资料,我想在登录后让他在那里,不定向他到 $这个 - > 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,以获得$ C $ c。在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 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.
下面,我已经张贴的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.
推荐答案
添加父:: 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->重定向()返回用户所采取的登录页面之前,登陆网址或Auth-> loginRedirect。
Auth->redirect() returns the url where the user landed before being taken to the login page or Auth->loginRedirect.
这篇关于CakePHP的验证组件重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!