CakePHP Auth 组件重定向问题
我无法让 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('*');
}
}
我似乎无法理解我做错了什么。
I am having trouble getting the Auth component do the redirects I want in a CakePHP 1.2.6 app.
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.
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());
}
/* [...] */
}
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加父级::beforeFilter();到用户控制器中的 beforeFilter :
您还可以将重定向替换为用户控制器的登录方法:
Auth->redirect() 返回用户在进入登录页面之前登陆的 url 或 Auth->redirect() 。登录重定向。
Add parent::beforeFilter(); to beforeFilter in the user controller:
You can also replace the redirect with this to the login method of your user controller:
Auth->redirect() returns the url where the user landed before being taken to the login page or Auth->loginRedirect.
将此代码放入您的控制器:
并将其添加到登录页面:
Put this code to your controller:
and, add this for the login page: