PostsController.php:(view()中的$this->set('post',...)中的post在视图中哪里有显示?)<?php
class PostsController extends AppController {
public $name = 'Posts';
public $helpers = array('Html', 'Form');

public function index() {
$this->set('posts', $this->Post->find('all'));
}

public function view($id = null) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}

?>
Post.php:<?php
class Post extends AppModel {
public $name = 'Post';

?>
index.ctp:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<h1>Blog posts</h1>
    <table>
     <tr>
         <th>Id</th>
            <th>Title</th>
            <th>Created</th>
        </tr>
        
        <!-- Here is where we loop through our $posts array, printing out post info -->
        <?php foreach ($posts as $post): ?>
        <tr>
         <td><?php echo $post['Post']['id']; ?></td>
            <td>
             <?php echo $this->Html->link($post['Post']['title'], // 链接文本
                 array('controller' => 'posts', 'action' => 'view', $post['Post']['id'], // 整个array表示http://localhost/posts(controller)/view(action)/1($post['Post']['id'])
'?' => array('height' => 40, 'width' => 50), // 链接地址后的"?"参数
'full_base' => true), //full_base表示绝对地址
array('class' => 'alink', 'target' => '_blank'),
'Are you sure you wish to view this post?'); //弹出javascript:confirm() ?>
            </td>
            <td><?php echo $post['Post']['created']; ?></td>
        </tr>
        <?php endforeach; ?>
        <?php unset($post); ?>
    </table>
</body>
</html>
view.ctp:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<h1><?php echo $post['Post']['title']; ?></h1>
<p><small>Created: <?php echo $post['Post']['created']; ?></small></p>
<p><?php echo $post['Post']['body']; ?></p>
</body>
</html>

解决方案 »

  1.   

    $this->set('post', $this->Post->read());<h1><?php echo $post['Post']['title']; ?></h1>
      

  2.   


    不是哦,这个$post是index.ctp中的foreach ($posts as $post)
      

  3.   

    请各位懂cakephp的高手帮一下忙嘛哪里有cakephp的函数手册?
      

  4.   


    http://cakephp.org/
      

  5.   

    你那view.ctp中 $post  不就是 吗 ???那你 index中应该是 $posts吧
      

  6.   


    对啊,index中是$posts
      

  7.   

    哦。。还有一个问题哦为什么public function view($id = null) {中$id要为null?我试过不设置这个变量也可以的,但官方手册上这样写是什么意思呢?
      

  8.   

    public $helpers = array('Html', 'Form');这个也一样,这样标明出来有什么用途?