Cakephp 如何使用视图和控制器

Cakephp 如何使用视图和控制器,cakephp,Cakephp,我试图学习CakePHP是如何工作的,但我被卡住了,我只想显示用户名。我在良好的控制器和良好的视野下工作。。。我不明白 视图=>Users=>index.ctp <h1>Users List</h1> <table> <tr> <th>Username</th> </tr> <?php foreach ($users as $k): ?> <

我试图学习CakePHP是如何工作的,但我被卡住了,我只想显示用户名。我在良好的控制器和良好的视野下工作。。。我不明白

视图=>Users=>index.ctp

<h1>Users List</h1> <table>

    <tr>
        <th>Username</th>

    </tr>

   <?php foreach ($users as $k): ?>

    <tr>

        <td><?php echo $k['username']; ?></td> 

    </tr> 

    <?php endforeach; ?> 

    </table>
我在视图中遇到的错误:

Notice (8): Undefined variable: users [APP\View\Users\index.ctp, line 9]

Warning (2): Invalid argument supplied for foreach() [APP\View\Users\index.ctp, line 9]

Notice (8): Undefined variable: users [APP\View\Users\index.ctp, line 13]

Warning (2): Invalid argument supplied for foreach() [APP\View\Users\index.ctp, line 13]

应首先在控制器中设置用户变量,如下所示:

  <?php
    class UsersController extends AppController{

        public function index() {
        $users = $this->User->find('all',array('fields' => array('id','username','created')));
        debug($users);
        $this->set('users', $users);
        }

    }
    ?>
在view index.ctp中,按如下方式操作:

<h1>Users List</h1> 
<table>

    <tr>
        <th>Username</th>

    </tr>

   <?php foreach ($users as $k): ?>

    <tr>

        <td><?php echo $k['User']['username']; ?></td> 

    </tr> 

    <?php endforeach; ?> 

    </table>

检查,您错过了将数据从控制器传递到视图的重要步骤。我将链接保留在您必须阅读的地方。谢谢您的回答。我知道我必须为每个视图创建一个控制器,我必须做其他事情?是:将数据从控制器传递到视图。。。代码就在我之前在评论中给出的链接中。是的,我读过,但我不明白我必须在代码中添加什么。你是说变量$helper?
  <?php
    class UsersController extends AppController{

        public function index() {
        $users = $this->User->find('all',array('fields' => array('id','username','created')));
        debug($users);
        $this->set('users', $users);
        }

    }
    ?>
<h1>Users List</h1> 
<table>

    <tr>
        <th>Username</th>

    </tr>

   <?php foreach ($users as $k): ?>

    <tr>

        <td><?php echo $k['User']['username']; ?></td> 

    </tr> 

    <?php endforeach; ?> 

    </table>