基于CakePHP实现的简单博客系统实例


Posted in PHP onJune 28, 2015

本文实例讲述了基于CakePHP实现的简单博客系统。分享给大家供大家参考。具体实现方法如下:

PostsController.php文件:

<?php
class PostsController extends AppController {
 public $helpers = array('Html', 'Form', 'Session');
 public $components = array('Session');
 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());
 }
 public function add()
 {
   if($this->request->is("post"))
   {
    $this->Post->create();
    if($this->Post->save($this->request->data))
    {
      $this->Session->setFlash("your post added!");
      $this->redirect(array('action'=>'index'));
    }
    else
    {
      $this->Session->setFlash("unable to create post!");
    }
   }
 }
 public function edit($id=null)
 {
   $this->Post->id=$id;
   if($this->request->is('get'))
   {
     $this->request->data = $this->Post->read();
   }
   else
   {
     if($this->Post->save($this->request->data)) 
     {
       $this->Session->setFlash('Your post has been updated.');
       $this->redirect(array('action' => 'index'));
     }
     else
     {
       $this->Session->setFlash('Unable to update your post.');
     }
   }
 }
 public function delete($id) {
    if ($this->request->is('get')) {
        throw new MethodNotAllowedException();
    }
    if ($this->Post->delete($id)) {
      $this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
 }
}
?>

Post.php文件:

<?php
class Post extends AppModel {
public $validate = array(
 'title' => array(
 'rule' => 'notEmpty'
),
 'body' => array(
 'rule' => 'notEmpty'
)
);
}
?>

routes.php文件:

<?php
/**
 * Routes configuration
 *
 * In this file, you set up routes to your controllers and their actions.
 * Routes are very important mechanism that allows you to freely connect
 * different urls to chosen controllers and their actions (functions).
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright   Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link     http://cakephp.org CakePHP(tm) Project
 * @package    app.Config
 * @since     CakePHP(tm) v 0.2.9
 * @license    MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 * Here, we are connecting '/' (base path) to controller called 'Pages',
 * its action called 'display', and we pass a param to select the view file
 * to use (in this case, /app/View/Pages/home.ctp)...
 */
  //Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
/**
 * ...and connect the rest of 'Pages' controller's urls.
 */
  Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
 * Load all plugin routes. See the CakePlugin documentation on 
 * how to customize the loading of plugin routes.
 */
  CakePlugin::routes();
/**
 * Load the CakePHP default routes. Only remove this if you do not want to use
 * the built-in default routes.
 */
  require CAKE . 'Config' . DS . 'routes.php';

blog.sql文件如下:

-- MySQL dump 10.13 Distrib 5.5.19, for Win64 (x86)
--
-- Host: localhost  Database: facebook
-- ------------------------------------------------------
-- Server version  5.5.19
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
 
--
-- Table structure for table `posts`
--
DROP TABLE IF EXISTS `posts`;
/*!40101 SET @saved_cs_client   = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `posts` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `title` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
 `body` text COLLATE utf8_unicode_ci,
 `created` datetime DEFAULT NULL,
 `modified` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `posts`
--
LOCK TABLES `posts` WRITE;
/*!40000 ALTER TABLE `posts` DISABLE KEYS */;
INSERT INTO `posts` VALUES (1,'The title','This is the post body.','2012-11-01 15:43:41',NULL),(2,'A title once again','And the post body follows.','2012-11-01 15:43:41',NULL),(3,'Title strikes back','This is really exciting! Not.','2012-11-01 15:43:41',NULL),(4,'ggjjkhkhhk','7777777777777777777777777\r\n777777777777777777777777','2012-11-01 20:16:28','2012-11-01 20:16:28');
/*!40000 ALTER TABLE `posts` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `schema_migrations`
--
DROP TABLE IF EXISTS `schema_migrations`;
/*!40101 SET @saved_cs_client   = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `schema_migrations` (
 `version` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 UNIQUE KEY `unique_schema_migrations` (`version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `schema_migrations`
--
LOCK TABLES `schema_migrations` WRITE;
/*!40000 ALTER TABLE `schema_migrations` DISABLE KEYS */;
INSERT INTO `schema_migrations` VALUES ('20121013024711'),('20121013030850');
/*!40000 ALTER TABLE `schema_migrations` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2012-11-01 16:41:46

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
PHP概述.
Oct 09 PHP
某大型网络公司应聘时的笔试题目附答案
Mar 27 PHP
用php过滤危险html代码的函数
Jul 22 PHP
php 字符串替换的方法
Jan 10 PHP
Laravel中扩展Memcached缓存驱动实现使用阿里云OCS缓存
Feb 10 PHP
PHP SPL标准库之数据结构栈(SplStack)介绍
May 12 PHP
Yii中实现处理前后台登录的新方法
Dec 28 PHP
PHP实现Google plus的好友拖拽分组效果
Oct 21 PHP
php中str_pad()函数用法分析
Mar 28 PHP
PHP asXML()函数讲解
Feb 03 PHP
php实现的顺序线性表示例
May 04 PHP
PHPstorm激活码2020年5月13日亲测有效
Sep 17 PHP
Codeigniter的dom类用法实例
Jun 26 #PHP
PHP关联数组实现根据元素值删除元素的方法
Jun 26 #PHP
PHP实现事件机制实例分析
Jun 26 #PHP
php使用MySQL保存session会话的方法
Jun 26 #PHP
Linux操作系统安装LAMP环境
Jun 26 #PHP
PHP中Session可能会引起并发问题
Jun 26 #PHP
WAMP环境中扩展oracle函数库(oci)
Jun 26 #PHP
You might like
PHP4实际应用经验篇(1)
2006/10/09 PHP
hessian 在PHP中的使用介绍
2010/12/13 PHP
PHP操作MongoDB时的整数问题及对策说明
2011/05/02 PHP
php实现单链表的实例代码
2013/03/22 PHP
PHP打开和关闭文件操作函数总结
2014/11/18 PHP
Yii2增删改查之查询 where参数详细介绍
2016/08/08 PHP
PHP使用redis消息队列发布微博的方法示例
2017/06/22 PHP
Smarty模板变量与调节器实例详解
2019/07/20 PHP
js小技巧--自动隐藏红叉叉
2007/08/13 Javascript
web基于浏览器的本地存储方法应用
2012/11/27 Javascript
如何编写高质量JS代码
2014/12/28 Javascript
javascript实现校验文件上传控件实例
2015/04/20 Javascript
Validform+layer实现漂亮的表单验证特效
2016/01/17 Javascript
特殊日期提示功能的实现方法
2016/06/16 Javascript
JS实现对中文字符串进行utf-8的Base64编码的方法(使其与Java编码相同)
2016/06/21 Javascript
前端JS面试中常见的算法问题总结
2016/12/23 Javascript
NodeJS链接MySql数据库的操作方法
2017/06/27 NodeJs
react build 后打包发布总结
2018/08/24 Javascript
微信小程序实现蒙版弹出窗功能
2019/09/17 Javascript
微信小程序表单验证WxValidate的使用
2019/11/27 Javascript
使用pkg打包ThinkJS项目的方法步骤
2019/12/30 Javascript
python实现查询IP地址所在地
2015/03/29 Python
Python读写txt文本文件的操作方法全解析
2016/06/26 Python
通过5个知识点轻松搞定Python的作用域
2016/09/09 Python
Python中pillow知识点学习
2018/04/30 Python
解决启动django,浏览器显示“服务器拒绝访问”的问题
2020/05/13 Python
html5的websockets全双工通信详解学习示例
2014/02/26 HTML / CSS
韩国休闲女装品牌网站:ANAIS
2016/08/24 全球购物
意大利高端时尚买手店:Stefania Mode
2018/03/01 全球购物
Myprotein比利时官方网站:欧洲第一运动营养品牌
2020/10/04 全球购物
会计专业自荐信范文
2013/12/02 职场文书
公司清洁工岗位职责
2013/12/14 职场文书
心得体会怎么写
2013/12/30 职场文书
毕业感言怎么写
2015/07/31 职场文书
SQL语法CONSTRAINT约束操作详情
2022/01/18 MySQL
Elasticsearch 配置详解
2022/04/19 Java/Android