2014-02-12 74 views
1
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> 

這isthe在我的控制器代碼登錄功能:

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

我正在使用「UsersController」 –

回答

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> 

,然後在下面的功能應該是在應用程序/控制器/ 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,以瞭解更多信息,您應該看到this