摘抄cakephp-blog

cakephp实例-blog最新版

 
开始创建我们的第一个cakephp实例:简单的blog。
 

第一步:创建的数据库


首先,我们只建立一个表用于存放文章,并在里面增加一些简单文章,以便初始显示。

在你的数据库里执行以下SQL:
/* 首先建立表: */
CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
text TEXT,
modified DATETIME DEFAULT NULL
);
/* 添加一些简单文章 */
INSERT INTO posts (title,text , modified)
VALUES ('The title', 'This is the post body.', NOW());
INSERT INTO posts (title, text, modified)
VALUES ('A title once again', 'And the post body follows.', NOW());
INSERT INTO posts (title, text, modified)
VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());

  

第二步:建立模型文件(Models)


 

在/cakephp/myapp/app/models文件夹里创建post.php(这里说明下,在cakephp中,表明是复数、模型文件名称是表的单数形式,控制器文件名称是复数形式,如这里的,表名是posts、模型文件名称是post.php、而控制器名称是posts_controller.php,控制器文件名称必须在是表名_controller.php的格式)。

我们建立的模型文件的完整内容如下:
<?php
class Post extends AppModel {
  var $name = 'Post';
}
?>

是不是很简单呢。^_^

 

第三步:建立控制器(Controller)

 


在/cakephp/myapp/app/controllers文件夹里创建posts_controller.php控制器文件,控制器是控制所有动作的集中地方,我们在这里主要是针对显示、增加、删除、修改创建action,完整内容如下:
<?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());
  }
//创建一个读取具体一篇文章的action  
  function read($id = null) {
  if (!$id) {
  $this->Session->setFlash('Invalid Post.');
  $this->redirect(array('action'=>'index'), null, true);
  }
  $this->set('post', $this->Post->read(null, $id));
  }
//创建添加新文章的action 
  function write() {
  if (!empty($this->data)) {
  $this->cleanUpFields();
  $this->Post->create();
  if ($this->Post->save($this->data)) {
  $this->Session->setFlash('The Post has been saved');
  $this->redirect(array('action'=>'index'), null, true);
  } else {
  $this->Session->setFlash('The Post could not be saved. Please, try again.');
  }
  }
  }
//创建删除文章的action,参数ID,这里的删除我们只执行一些逻辑上的操作,因此不需要建立视图文件
  function delete($id)
  {
  $this->Post->del($id);
  $this->flash('The post with id: '.$id.' has been deleted.', '/posts');
  }
//创建编辑文章的action
  function edit($id = null)
  {
  if (empty($this->data))
  {
  $this->Post->id = $id;
  $this->data = $this->Post->read();
  }
  else
  {
  if ($this->Post->save($this->data['Post']))
  {
  $this->flash('Your post has been updated.','/posts');
  }
  }
  }
}
?>

我们不必去管上面的代码的具体意思,我也没弄太明白^_^,以后的学习中,我会和大家一起来进行具体了解

 

第四步:建立视图(Views)

 


1、 建立列表视图:/cakephp/myapp/app/views/index.thmtl(因为刚才在控制器中我们建立了一个index的action,因此这里的视图名称是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>

现在我们可以通过浏览http://localhost/posts/ 或者 http://localhost/posts/index 查看文章列表了,希望你能看到这样的画面。

 


 

但是现在的连接还不能用,点title内容是查看具体内容,点Delete是删除改文章,点击edit是编辑改文章,因为我们现在还没有创建增加、查看、修改的视图,因此现在点击这些连接将会显示错误,但是删除连接是可以用的,因为此cation是不用创建视图的,接下来我们就开始依次创建这些视图

 

2、 创建增加的视图:/cakephp/myapp/app/views/write.thtml

 
<h1>Add Post</h1>
<form method="post" action="<?php echo $html->url('/posts/write')?>">
  <p>
  Title:
  <?php echo $form->input('Post/title', array('size' => '40'))?>
  <?php echo $form->error('Post/title', 'Title is required.') ?>
  </p>
  <p>
  Body:
  <?php echo $form->textarea('Post/text', array('rows'=>'10')) ?>
  <?php echo $form->error('Post/text', 'Body is required.') ?>
  </p>
  <p>
  <?php echo $form->submit('Save') ?>
  </p>
</form>

保存好文件,现在我们可以点击:Add Post到达增加文章界面进行文章的添加了。

 

3、 创建查看视图/cakephp/myapp/app/views/read.thmtl
<h1><?php echo $post['Post']['title']?></h1>
<p><small>Created: <?php echo $post['Post']['modified']?></small></p>
<p><?php echo $post['Post']['text']?></p>

现在我们就可以点击标题的连接来查看具体内容了,浏览地址将是:

http://localhost/post/read/1,这里的1就是传入的参数

 

4、 创建修改视图:/cakephp/myapp/app/views/edit.thmtl
<h1>Edit Post</h1>
<form method="post" action="<?php echo $html->url('/posts/edit')?>">
  <?php echo $form->hidden('Post/id'); ?>
  <p>
  Title:
  <?php echo $form->input('Post/title', array('size' => '40'))?>
  <?php echo $form->error('Post/title', 'Title is required.') ?>
  </p>
  <p>
  Body:
  <?php echo $form->textarea('Post/text', array('rows'=>'10')) ?>
  <?php echo $form->error('Post/text', 'Body is required.') ?>
  </p>
  <p>
  <?php echo $form->submit('Save') ?>
  </p>
</form>

到现在为止,所有的视图均已经成功创建,现在就可以完整的使用这个简单的blog了。

 

在做这个实例的时候,我开始是参照IBM的文章来看的,但是发现存在一点问题,就是在视图里面使用<h2><?php echo $html->link("It's BLAHG!", array('action'=>'index')); ?></h2>这种连接形式的时候,会报错误:Notice: Array to string conversion。没找到原因,如果有人知道这是什么原因造成的,还希望能告诉我。

 

在接下来的日志中,我会和大家一起分享解读手册的一些经验,也希望大家能给我一些学习经验。

 (版本不同view下的文件后缀名也可能不同,我的默认是ctp,有时会有这样的错误)

原文http://blog.csdn.net/wjazz/archive/2008/07/07/2622976.aspx

对其里面的小错误进行了调试

原文地址:https://www.cnblogs.com/rootfaker/p/6805992.html