rubyonrails:攻克CakePHP(PHP中的Ruby On Rails框架)图文介绍

增加Cotroller模块:app/controllers/posts_controller.php

代码:

<?php
require_once('cake/app_controller.php');
PostsControllerextendsAppController {
public$name='Posts';
functionindex{
$this->('posts',$this->Post->findAll);
}

functionview($id=null){
$this->Post->id=$id;
$this->('post',$this->Post->read);
}

functionadd{
(!empty($this->data)){
($this->Post->save($this->data)){
//$this->flash('Your post has been saved.','/posts');
$this->redirect("/posts/index");
}
}
}

functiondelete($id){
$this->Post->del($id);
//$this->flash('The post with id: '.$id.' has been deleted.', '/posts');
$this->redirect("/posts/index");
}

functionedit($id=null){
(empty($this->data)){
$this->Post->id=$id;
$this->data=$this->Post->read;
}
{
($this->Post->save($this->data['Post'])){
//$this->flash('Your post has been updated.','/posts');
$this->redirect("/posts/index");
}
}
}
}
?>

增加页面模块:

/app/views/下追加posts文件夹,然后再添加4个页面(般后缀为.rhtml)

代码依次为:

index.thtml

<h1>Blog posts</h1>
<p><?php echo $html->link("Add Post", "/posts/add");?>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>

<!--Here's where we loop through our $posts .gif' />, pring out post info-->

<?php foreach ($posts as $post):?>
<tr>
<td><?php echo $post['Post']['id'];?></td>
<td>
<?php echo $html->link($post['Post']['title'], '/posts/view/'.$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>
<td><?php echo $post['Post']['created'];?></td>
</tr>
<?php endforeach;?>

</table>

view.thtml

<h1><?php echo $post['Post']['title']?></h1>
<p><small>Created:<?php echo $post['Post']['created']?></small></p>
<p><?php echo $post['Post']['body']?></p>
<br>
<p><?php echo $html->link('Return', '/posts/index/');?></p>

edit.thtml

<h1>Edit Post</h1>
<formmethod="post"action="<?php echo $html->url('/posts/edit')?>">
<?php echo $html->hidden('Post/id');?>
<p>
Title:
<?php echo $html->input('Post/title', .gif' />('size' => '40'))?>
<?php echo $html->tagErrorMsg('Post/title', 'Title is required.')?>
</p>
<p>
Body:
<?php echo $html->textarea('Post/body', .gif' />('rows'=>'10'))?>
<?php echo $html->tagErrorMsg('Post/body', 'Body is required.')?>
</p>
<p>
<?php echo $html->submit('Save')?>
</p>
</form>

add.thtml

<h1>Add Post</h1>
<formmethod="post"action="<?php echo $html->url('/posts/add')?>">
<p>
Title:
<?php echo $html->input('Post/title', .gif' />('size' => '40'))?>
<?php echo $html->tagErrorMsg('Post/title', 'Title is required.')?>
</p>
<p>
Body:
<?php echo $html->textarea('Post/body', .gif' />('rows'=>'10'))?>
<?php echo $html->tagErrorMsg('Post/body', 'Body is required.')?>
</p>
<p>
<?php echo $html->submit('Save')?>
</p>
</form>

测试URL:http://localhost/phpblog/posts/
参考网页:http://manual.cakephp.org/appendix/blog_tutorial
以下是CakePHP框架些特性:http://www.lostk.com/blog/cakephp_note/

e="COLOR: #000000">
Title:
<?php echo $html->input('Post/title', .gif' />('size' => '40'))?>
<?php echo $html->tagErrorMsg('Post/title', 'Title is required.')?>
</p>
<p>
Body:
<?php echo $html->textarea('Post/body', .gif' />('rows'=>'10'))?>
<?php echo $html->tagErrorMsg('Post/body', 'Body is required.')?>
</p>
<p>
<?php echo $html->submit('Save')?>
</p>
</form>
TAG: cakephp CakePHP rails Rails ruby Ruby 攻克 框架


Tags:  rubyonrails中文 rubyonrails教程 rubyrails rubyonrails

延伸阅读

最新评论

发表评论