I have been on this login part of my program for a while but i have just been failing..I am just so tired of this thing...Please how do you do login controller and view for cakephp?...I have tried and tried but it still doesnt work...please HELP!!!
这是我的登录view.ctp
<html>
<body background="bgimage.jpg">
<center>
<strong><h2>LOGIN FORM</h2><strong>
<?php
echo $this->form->create('Login', array('action' => 'login'));
echo $this->form->input('Username'); ?> <br>
<?php echo $this->form->input('Password'); ?> <br>
<?php echo $this->form->end('Login');
?>
<br><br>
<?php echo $this->html->link ('Forgot Password?',array('action'=>'forgot')); ?>
</center>
<p><?php echo $this->html->link ('Create Account', array('action' =>'add')); ?></p>
</body>
</html>
这是我的控制器代码中的登录功能:
function login() {
$this->loadModel('User');
$this->request->data['MyModel']['username'];
$this->request->data['MyModel']['title'];
$this->redirect(array('action' => 'home'));
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl('home'));
// `return $this->redirect($this->Auth->redirect());`
} else {
$this->Session->setFlash(
__('Username or password is incorrect'),
'default',
array(),
'auth'
);
}
}
请问,我做错了什么? 请问您如何登录控制器并查看cakephp?
答案 0 :(得分:0)
首先,下面的代码应该在app / View / Users / login.ctp中,而不是view.ctp,并使用下面的代码,看看我做的一些调整。
<html>
<body background="bgimage.jpg">
<center>
<strong><h2>LOGIN FORM</h2><strong>
<?php
echo $this->form->create('User', array('action' => 'login'));
echo $this->form->input('username'); // please use your 'username' as you put in the database?> <br>
<?php echo $this->form->input('password'); // please use your 'password' as you put in the database ?> <br>
<?php echo $this->form->end('Login');
?>
<br><br>
<?php echo $this->html->link ('Forgot Password?',array('action'=>'forgot')); ?>
</center>
<p><?php echo $this->html->link ('Create Account', array('action' =>'add')); ?></p>
</body>
</html>
然后在下面的函数应该在app / Controller / UsersController.php
中function login() {
//$this->loadModel('User'); no need to use this as you are in UsersController.php
//$this->request->data['MyModel']['username']; you don't need this
//$this->request->data['MyModel']['title']; you don't need this
//$this->redirect(array('action' => 'home')); why do you want to redirect here, if you redirect here, it'll not come to login page at first
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect(array('action' => 'home'));//redirect should be here
//return $this->redirect($this->Auth->redirectUrl('home'));
// `return $this->redirect($this->Auth->redirect());`
} else {
$this->Session->setFlash(
__('Username or password is incorrect'),
'default',
array(),
'auth'
);
}
}
在此之前,你应该在你的AppController.php中有'Auth'组件,有关更多信息,你应该看到this