使用表单帮助器时出现未定义错误


cakeCake PHP : Undefined error when using Form helper

我是cakePHP新手。我只是想创建一个简单的表单,在视图上有一个文本框和一个提交按钮,并在控制器上使用post数据显示文本。

My View Code:

<?php echo $form->create(null, array('action' => 'index'));?>
<fieldset><legend>Enter Your Name</legend><?php echo $form->input('name'); ?></fieldset>
<?php echo $form->end('Go');?> 

控制器代码:

<?php
class UsersController extends AppController  {
        var $name = 'Users';
        var $uses = array();
        var $helpers = array('Html','Form');
        function index() {
                if ( !empty($this->data) ) {
                        echo $this->data['name'];
                        $this->autoRender = false;
                }
        }
}
?>

我收到错误,

Notice (8): Undefined variable: form [APP/View/Users/index.ctp, line 1]
Code Context
include - APP/View/Users/index.ctp, line 1
View::_render() - CORE/Cake/View/View.php, line 598
View::render() - CORE/Cake/View/View.php, line 365
Controller::render() - CORE/Cake/Controller/Controller.php, line 900
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 114
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 89
[main] - APP/webroot/index.php, line 96

这是什么问题

$form->method来自旧版本的CakePHP。在新版本中,表单帮助程序是$this->Form->method。因此,只需在代码中替换这些实例:

<?php echo $this->Form->create(null, array('action' => 'index'));?>
<fieldset><legend>Enter Your Name</legend><?php echo $this->Form->input('name'); ?></fieldset>
<?php echo $this->Form->end('Go');?> 

像这样

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