我的auth组件工作得很好,除了它复制了我的CakePHP所在的文件夹。例如,我的整个CakePHP安装位于localhost/rh/
但是当登录重定向时,它会将用户发送到localhost/rh/rh/controller
。有什么想法吗?
AppController的:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'index'),
'authError' => "You are not authorized to access that page",
'authorize' => array('Controller')
)
);
public function isAuthorized($user) {
return true;
}
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
UserController中:
class UsersController extends AppController {
//before filter to allow users to register
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add'); // Letting users register themselves
}
//login action
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
//logout action
public function logout() {
$this->redirect($this->Auth->logout());
}
答案 0 :(得分:1)
添加parent :: beforeFilter();到用户控制器中的beforeFilter:
function beforeFilter() {
$this->Auth->autoRedirect = false;
parent::beforeFilter();
}
您也可以将重定向替换为用户控制器的登录方法:
$this->redirect($this->Auth->redirect());
Auth->redirect() returns
要获得更清晰的想法,请转到cakephp.org link
答案 1 :(得分:0)
添加parent :: beforeFilter();到用户控制器中的beforeFilter:
function beforeFilter()
{
$this->Auth->autoRedirect = false;
parent::beforeFilter();
}
您还可以将重定向替换为用户控制器的登录方法: dd parent :: beforeFilter();到用户控制器中的beforeFilter:
function beforeFilter()
{
$this->Auth->autoRedirect = false;
parent::beforeFilter();
}
您也可以将重定向替换为用户控制器的登录方法:
$this->redirect($this->Auth->redirect());
Auth-> redirect()返回用户在进入登录页面或Auth-> loginRedirect之前登陆的网址。
答案 2 :(得分:0)
Auth组件中有一个名为“unauthorizedRedirect”的选项,它是用户在尝试访问不允许访问的页面时重定向用户的URL。如果未设置,则Cake重定向到/ {app-directory},因此您的域名将出现在控制器所在的位置。
在AppController中更改此内容
public $components = array(
//your other components
'Auth' => array(
//your other options for Auth
'unauthorizedRedirect' => 'url to redirect to' //can be in any form that Cake accepts urls in
)
);