将webroot下的所有网页重定向到Cakephp中的登录页面
我使用的是CakePHP2.4.5。我想将所有未登录的用户重定向到登录页面。我基本上遵循了可用的说明 总之,重要的部分是AppController.php的以下代码将webroot下的所有网页重定向到Cakephp中的登录页面,php,cakephp,redirect,login,cakephp-2.0,Php,Cakephp,Redirect,Login,Cakephp 2.0,我使用的是CakePHP2.4.5。我想将所有未登录的用户重定向到登录页面。我基本上遵循了可用的说明 总之,重要的部分是AppController.php的以下代码 public $components = array('Session', 'Auth' => array( 'loginRedirect' => array('controller' => 'u
public $components = array('Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authError' => 'You must be logged in to view this page.',
'loginError' => 'Invalid Username or Password entered, please try again.'
));
任何具有此URL格式的网站http://localhost/cakephp245/controllers/XXX
将被重新定向到登录页面。但是,位于app/webroot
中的网站的URL类似于http://localhost/cakephp245/app/webroot/XXX
将不会被重新定向到登录页面
如何强制位于app/webroot文件夹内的网站重新定向到登录页面
非常感谢。将此功能添加到AppController
public function beforeFilter() {
$this->Auth->deny();
$this->Auth->allow('login');
}
这样,登录之前唯一允许的操作就是登录本身。
不过,这不会使图像、脚本或css不可用,如果这是您的目标的话
虽然我不能完全确定,但我相信没有办法拒绝某人访问此类资源。以下是有助于解决问题的步骤:- 1) 阅读如何在appController中加载auth组件的文档
代码应该与下面的代码类似
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'plugin' => null
],
//'authorize' => ['Controller'],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'dashboard'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login',
],
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email', 'password' => 'password']
]
],
'unauthorizedRedirect' => false,
'authError' => 'Did you really think you are allowed to see that?',
'storage' => 'Session'
]);
2) 将以下代码添加到usersController的beforeFilter()
$this->Auth->allow(['login','logout','register']); // these function will be pulic access
public function login()
{
$this->viewBuilder()->layout('adminlogin'); // set the admin login layout
$user = $this->Users->newEntity();
$this->set('user', $user);
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user){
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}else{
$this->Flash->error(__('Invalid username or password, try again'));
}
}
}
3) 下面是登录函数,将其放在UserController中
$this->Auth->allow(['login','logout','register']); // these function will be pulic access
public function login()
{
$this->viewBuilder()->layout('adminlogin'); // set the admin login layout
$user = $this->Users->newEntity();
$this->set('user', $user);
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user){
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}else{
$this->Flash->error(__('Invalid username or password, try again'));
}
}
}
位于webroot?中的网站:s