我刚开始使用cakePHP进行编码。我编码并尝试编写登录和身份验证页面。
我试过这个:
class AppController extends Controller
{
public function beforeFilter()
{
parent::beforeFilter();
if ($this->request->action != 'login' && !$this->Session->check('profile'))
{
$this->Session->setFlash('You are not logged in');
$this->redirect(array(
'controller' => 'profiles',
'action' => 'login'
));
}
}
}
我的问题是我无法登录。我总是得到
"您尚未登录"
如果没有此代码,它可以正常工作,但不会保护其他页面
我的routes.php
Router::connect('/', array('controller' => 'profiles', 'action' => 'login'));
我的个人资料控制器.php
public function login()
{
if ($this->request->is('post'))
{
$profile = $this->Profile->find('first', array(
'conditions' => array(
'name' => $this->request->data('Profile.name'),
'password' => $this->request->data('Profile.password')
)
));
if ($profile)
{
$this->Session->write('Profile',$profile);
$this->redirect(array('controller' => 'Projects',
'action' => 'index'));
}
$this->Session->setFlash('Profile and Password does not match!');
}
}
答案 0 :(得分:3)
看起来您没有加载auth组件,而您实际上并没有登录用户。
在尝试创建自己的身份验证函数之前,您可能需要考虑内置身份验证功能。
有关身份验证的文档。
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
了解Cake身份验证的教程。这很简单。
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html