cakephp中表名是复数形式例如'posts',models文件名是表单数形式'post.php',controller文件名是复数形式,而且是XX表s_controller.php结尾,'posts_controller.php',view里页面的命名是源于_controller.php里的设定,
路径是 localhost/文件夹名/ 类名/方法名,如localhost/mycake/posts/index
models文件夹里的文件都是和数据库连接有关的,也就是和表有关的东西。
models/
post.php中
<?php
class Post extends AppModel {
var $name = 'Post'; //-->这个Post是view中需要用到的表名,view中Post/id代表id字段,Post/title代表title字段
//也就是views中<?php echo $form->hidden('Post/id'); ?>
}
?>
class Post extends AppModel {
var $name = 'Post'; //-->这个Post是view中需要用到的表名,view中Post/id代表id字段,Post/title代表title字段
//也就是views中<?php echo $form->hidden('Post/id'); ?>
}
?>
controllers文件夹里都是和数据操作有关的东西,也就是各种PHP方法,function。
controllers/posts_controller.php
<?php
class PostsController extends AppController {
var $name = 'Posts';//表明复数,类名开头的词
//创建首页列表显示cation这里的index必须是和后面对应视图名字一样,视图名称就
//应该是index.thtml,在这里定义了index函数
//我们就可以通过http://localhost/posts/index来访问了此函数,以下同理,
function index() {
$this->set('posts', $this->Post->findAll());//变量posts映射到view页面
}
?>
class PostsController extends AppController {
var $name = 'Posts';//表明复数,类名开头的词
//创建首页列表显示cation这里的index必须是和后面对应视图名字一样,视图名称就
//应该是index.thtml,在这里定义了index函数
//我们就可以通过http://localhost/posts/index来访问了此函数,以下同理,
function index() {
$this->set('posts', $this->Post->findAll());//变量posts映射到view页面
}
?>
views文件夹里就是页面啦。
views/posts/index.thtml
<h1>Blog posts</h1>
<p><?php echo $html->link('Add Post', '/posts/write'); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $html->link($post['Post']['title'], "/posts/read/".$post['Post']['id']); ?>
<?php echo $html->link('Delete',"/posts/delete/{$post['Post']['id']}",null,'Are you sure');?>
<?php echo $html->link('edit',"/posts/edit/{$post['Post']['id']}"); ?>
</td>
<td><?php echo $post['Post']['modified']; ?></td>
</tr>
<?php endforeach; ?>
</table>
大致如此了
<p><?php echo $html->link('Add Post', '/posts/write'); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $html->link($post['Post']['title'], "/posts/read/".$post['Post']['id']); ?>
<?php echo $html->link('Delete',"/posts/delete/{$post['Post']['id']}",null,'Are you sure');?>
<?php echo $html->link('edit',"/posts/edit/{$post['Post']['id']}"); ?>
</td>
<td><?php echo $post['Post']['modified']; ?></td>
</tr>
<?php endforeach; ?>
</table>