将webroot下的所有网页重定向到Cakephp中的登录页面 [英] Redirect all webpages under webroot to login page in Cakephp
问题描述
我正在使用cakephp 2.4.5.我想将所有尚未登录的用户重定向到登录页面.我基本上遵循了此处.
I am using cakephp 2.4.5. I would like to redirect all users who have not logged in to a login page. I basically followed the instructions available here.
总而言之,重要的部分是AppController.php的以下代码
In summary, the important part is the following code to AppController.php
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
的网站将不会被重定向到登录页面.
Any websites with this URL format http://localhost/cakephp245/controllers/XXX
will be re-directed to the login page. However, websites that are located inside app/webroot
with URL that looks like this http://localhost/cakephp245/app/webroot/XXX
will not be re-directed to the login page.
如何强制将位于app/webroot文件夹中的网站重定向到登录页面?
How can I force websites located inside app/webroot folder to be re-directed to the login page?
非常感谢您.
推荐答案
下面是可以帮助解决问题的步骤:-
Below are the steps that can help to resolve the issues :-
1)阅读文档,了解如何在appController中加载auth componenet
https://book.cakephp.org/3.0/en/controllers/components/authentication.html
代码应类似于以下代码
1) Read the documentation how to load the auth componenet in appController
https://book.cakephp.org/3.0/en/controllers/components/authentication.html
Code should be like the below code
$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()
2) Add below code to beforeFilter() of usersController
$this->Auth->allow(['login','logout','register']); // these function will be pulic access
3)这是放在UserController中的登录功能
3) Here is the login function put it in UserController
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下的所有网页重定向到Cakephp中的登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!