简体   繁体   中英

Error with Form helper in CakePHP 2.0

I'm starting out with CakePHP 2.0 and can't get past a simple login. This is my code:

AppController:

class AppController extends Controller
{
    function beforeFilter()
    {
        $this->Auth->userModel = 'User';
        $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'hotels', 'action' => 'dashboard');
    }
}

UsersController:

class UsersController extends AppController
{
    var $name = 'Users';
    var $helpers = array('Html','Form');
    var $components = array('Auth','Session');

    function beforeFilter()
    {
        $this->Auth->allow("logout");
        parent::beforeFilter();
    }

    function logout()
    {
        $this->redirect($this->Auth->logout());
    }

    function login()
    {
        if ($this->Auth->login())
        {
            $this->redirect($this->Auth->redirect());
        } else
        {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

login.ctp:

 echo $this->Form->create(); //'User', array('action' => 'login'));
 echo $form->Form->input('email');
 echo $form->Form->input('password');
 echo $form->Form->end('Login');

The error I'm getting is this:

 Notice (8): Undefined variable: form [APP\View\Users\login.ctp, line 13]
 Notice (8): Trying to get property of non-object [APP\View\Users\login.ctp, line 13]
 Fatal error: Call to a member function input() on a non-object in E:\proyectos\web\swt\app\View\Users\login.ctp on line 13

Any ideas? Thanks in advance!

These

echo $form->Form->input('email');
echo $form->Form->input('password');
echo $form->Form->end('Login');

should be

echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->end('Login');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM