基于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 相关文章推荐
vBulletin HACK----关于排版的两个HACK
Oct 09 PHP
PHP伪静态页面函数附使用方法
Jun 20 PHP
php array_pop()数组函数将数组最后一个单元弹出(出栈)
Jul 12 PHP
php array_push()数组函数:将一个或多个单元压入数组的末尾(入栈)
Jul 12 PHP
php实现文件编码批量转换
Mar 10 PHP
php可扩展的验证类实例(可对邮件、手机号、URL等验证)
Jul 09 PHP
PHP 中 Orientation 属性判断上传图片是否需要旋转
Oct 16 PHP
基于PHP生成简单的验证码
Jun 01 PHP
PHP解耦的三重境界(浅谈服务容器)
Mar 13 PHP
PHP jQuery+Ajax结合写批量删除功能
May 19 PHP
PHP mongodb操作类定义与用法示例【适合mongodb2.x和mongodb3.x】
Jun 16 PHP
Yii框架安装简明教程
May 15 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
php中的时间显示
2007/01/18 PHP
PHP+Mysql+Ajax+JS实现省市区三级联动
2014/05/23 PHP
php绘制一条直线的方法
2015/01/24 PHP
PHP保留两位小数的几种方法
2019/07/24 PHP
TP5框架使用QueryList采集框架爬小说操作示例
2020/03/26 PHP
[对联广告] JS脚本类
2006/08/27 Javascript
JavaScript For Beginners(转载)
2007/01/05 Javascript
Jquery CheckBox全选方法代码附js checkbox全选反选代码
2010/06/09 Javascript
JavaScript通过RegExp实现客户端验证处理程序
2013/05/07 Javascript
js代码验证手机号码和电话号码是否合法
2015/07/30 Javascript
微信小程序实现指定显示行数多余文字去掉用省略号代替
2018/07/25 Javascript
Vue中使用ElementUI使用第三方图标库iconfont的示例
2018/10/11 Javascript
vue-dplayer 视频播放器实例代码
2019/11/08 Javascript
vue实现一个6个输入框的验证码输入组件功能的实例代码
2020/06/29 Javascript
基于原生js实现判断元素是否有指定class名
2020/07/11 Javascript
JS遍历树层级关系实现原理解析
2020/08/31 Javascript
[01:03:27]NAVI vs EG 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
详解Python中列表和元祖的使用方法
2015/04/25 Python
Python中生成器和迭代器的区别详解
2018/02/10 Python
利用Python将文本中的中英文分离方法
2018/10/31 Python
朴素贝叶斯Python实例及解析
2018/11/19 Python
python实现AES加密和解密
2019/03/27 Python
Django之富文本(获取内容,设置内容方式)
2020/05/21 Python
基于python实现生成指定大小txt文档
2020/07/20 Python
CSS3属性box-shadow使用指南
2014/12/09 HTML / CSS
意大利顶级奢侈品电商:LUISAVIAROMA(支持中文)
2020/05/26 全球购物
光电信息专业应届生求职信
2013/10/07 职场文书
2014年党员承诺书范文
2014/05/20 职场文书
航海技术专业毕业生推荐信
2014/07/09 职场文书
人事代理委托书
2014/09/27 职场文书
查摆问题自查报告范文
2014/10/13 职场文书
2015年学校办公室工作总结
2015/05/26 职场文书
高中团支书竞选稿
2015/11/21 职场文书
施工安全协议书
2016/03/22 职场文书
使用Html+Css实现简易导航栏功能(导航栏遇到鼠标切换背景颜色)
2021/04/07 HTML / CSS
详解在SQLPlus中实现上下键翻查历史命令的功能
2022/03/18 SQL Server