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>
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>
不是哦,这个$post是index.ctp中的foreach ($posts as $post)
http://cakephp.org/
对啊,index中是$posts