当我按照$this->Auth->allow('login');
此方法操作时,我们无法重定向到信息中心页面。如果我按照$this->Auth->allow();
此方法操作,我们就可以重定向到信息中心页面。
但我需要使用第一种方法登录。
我有一个AppController.php,它执行了Login Action。
以下是AppController.php代码
function beforeFilter() {
Security::setHash('md5');
// Authenticate
$this->Auth->authorize = 'Controller';
$this->Auth->autoRedirect = false;
$subdomain = substr( env("HTTP_HOST"), 0, strpos(env("HTTP_HOST"), ".") );
if($this->params['controller']=="moderator")
{
//$this->Auth->allow(); // if i use this one my code is working fine.
$this->Auth->allow('login');
$this->Auth->loginAction = array('controller' => 'moderator', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'moderator', 'action' => 'dashboard');
$this->Auth->loginError = 'No username and password was found with that combination.';
$this->Auth->logoutRedirect = array('controller' => 'moderator', 'action' => 'login');
AuthComponent::$sessionKey = 'Auth.Admin';
}
}
当我们登录页面时,它将转到ModeratorController.php控制器页面,然后使用验证检查用户名和密码,如果用户名&密码正确无误调用 信息中心功能。
以下是ModeratorController.php代码
class ModeratorController extends AppController {
public $components = array('RequestHandler','Session',
'Auth'=>array(
'authenticate' => array(
'Form' => array(
'fields'=>array(
'username'=>'username',
'password'=>'password'
),
'userModel'=> 'Admin'
),
),
'loginAction' =>array(
'Controller' => 'moderator',
'action' => 'login'
),
'loginRedirect'=>array('Controller'=>'moderator', 'action'=>'index'),
'logoutRedirect'=>array('Controller'=>'moderator', 'action'=>'index'),
'authError'=>"You can't access that page",
'authorize'=>'Controller',
'loginError'=> 'Login error',
)
);
public $helpers = array('Js' => array('Jquery'),'Html','Paginator');
function beforeFilter() {
//used version cake 2.3.0.
parent::beforeFilter();
}
// for moderator login process
public function login() {
if($this->Session->read('Auth.Admin.admin_id'))
{
$this->redirect('dashboard');
}
if($this->request->data)
{
$this->Admin->set($this->data);
if($this->Admin->validates())
{
if($this->Auth->login()){
//$this->redirect('/moderator/dashboard'); // result: redirect to moderator/login
//$this->redirect(array('controller' => 'moderator', 'action' => 'dashboard')); // result: redirect to moderator/login
//$this->redirect($this->Auth->redirect()); // result: redirect to moderator/login
//$this->redirect(array('action' => 'dashboard')) // result: redirect to moderator/login
//$this->redirect('dashboard12'); // result: redirect to moderator/dashboard12 and give me 404. That result is correct
//$this->redirect('/dashboard'); // result: redirect to /dashboard and give me 404. That result is also correct
$this->redirect('dashboard')) // result: redirect to moderator/login
}
else
{
$this->Session->setFlash(__('Invalid email or password, try again'));
}
}
}
}
public function dashboard()
{
echo "I am in dashboard";
exit();
}
}
由于
答案 0 :(得分:2)
我看到一些在技术上错误的事情:
在CakePHP中构建时,您应遵循其标准
您应该moderators
作为控制器,而不是moderator
您参考了dashboard.php
Is that a controller? or are you referring to the method
如果你有一个控制器名称ModeratorsController.php
和一个名为dashboard()
的方法,那么你可以像这样重定向:
$this->redirect(array('controller' => 'moderators', 'action' => 'dashboard'));
或者您甚至可以像这样重定向
$this->redirect('/moderators/dashboard');
如果你引用的dashboard.php
文件实际上是控制器,在这种情况下应该是DashboardsControler.php
,那么你必须重定向到
$this->redirect('controller' => 'dashboards');
或
$this->redirect('/dashboards');
答案 1 :(得分:0)
$this->redirect('dashboard');
应该是
$this->redirect(array('action' => 'dashboard'));
如果此操作也在仪表板控制器中。
永远不要使用字符串作为链接并重定向到应用的内部页面,只有外部链接才能使用字符串表示法。
此外,您是否有使用CookieComponent的具体原因?您的cookie内容根本不会被加密,您设置了大量的cookie。使用该组件。