Fatal error: Call to a member function create() on a non-object in cakephp login application

StackOverflow https://stackoverflow.com/questions/22632380

  •  20-06-2023
  •  | 
  •  

I develop a model,controller,view class.and placed it in required folders.but still facing the problem.

Model class:

<?php
class User extends AppModel {
    function validateLogin($data)
    {

        $user = $this->find(array('username' => $data['username'], 'password' => $data['password']), array('id', 'username'));

        if( empty($user) == false )
        {
            return $user;
        }

        return false;
    }
}
?>

Controller class:

<?php
class UsersController extends AppController {
    var $name = 'Users';
    var $helpers = array('Form');


    function index(){
    }
    function login_form() { }

    function login() {

        if(empty($this->data['User']['username']) == false)
        {

            if(($user = $this->User->validateLogin($this->data['User'])) != false)
            {

                $this->Session->setFlash('Thank you for logging in!');
                $this->Session->write('User', $user);

                $this->Redirect(array('controller' => 'Controller_name', 'action' => 'Action_name'));
                exit();
            }
            else
            {
                $this->Session->setFlash('Incorrect username/password!', true);
                $this->Redirect(array('action' => 'login_form'));
                exit();
            }
        }
    }

    function logout() {

        $this->Session->destroy();
        $this->Session->setFlash('You have been logged out!');


        $this->Redirect('/');
        exit();
    }
}
?>

view class:

<div class="login"> 
<h2>Login</h2>     
    <?php echo $form->create('User', array('action' => 'login'));?> 
        <?php echo $form->input('username');?> 
        <?php echo $form->input('password');?> 
        <?php echo $form->submit('Login');?> 
    <?php echo $form->end(); ?> 
</div> 

I created the database for this.but the problem is giving the fatal error like

Error: Call to a member function create() on a non-object

有帮助吗?

解决方案

Your form should be something like this

<?php echo $this->Form->create('User', array('action' => 'login'));?>

 <!-- Form elements go here -->

 <?php echo $this->Form->end(); ?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top