基于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 30 PHP
php生成缩略图示例代码分享(使用gd库实现)
Jan 20 PHP
PHP抓取、分析国内视频网站的视频信息工具类
Apr 02 PHP
Yii实现多按钮保存与提交的方法
Dec 03 PHP
Laravel 5框架学习之Blade 简介
Apr 08 PHP
CodeIgniter针对lighttpd服务器URL重写的方法
Jun 10 PHP
整理php防注入和XSS攻击通用过滤
Sep 13 PHP
Linux系统下使用XHProf和XHGui分析PHP运行性能
Dec 08 PHP
thinkPHP实现MemCache分布式缓存功能
Mar 23 PHP
PHP 匿名函数与注意事项详细介绍
Nov 26 PHP
Yii2框架实现利用mpdf创建pdf文件功能示例
Feb 08 PHP
详解PHP中curl_multi并发的实现
Jun 08 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连接MySQL的2种方法小结以及防止乱码
2014/03/11 PHP
phpstorm 配置xdebug的示例代码
2019/03/31 PHP
TP5框架实现签到功能的方法分析
2020/04/05 PHP
最佳的addEvent事件绑定是怎样诞生的
2011/10/24 Javascript
解析offsetHeight,clientHeight,scrollHeight之间的区别
2013/11/20 Javascript
Javascript函数式编程语言
2015/10/11 Javascript
使用jQuery给input标签设置默认值
2016/06/20 Javascript
Html中 IFrame的用法及注意点
2016/12/22 Javascript
利用JavaScript在网页实现八数码启发式A*算法动画效果
2017/04/16 Javascript
Three.js利用顶点绘制立方体的方法详解
2017/09/27 Javascript
基于vue2.0动态组件及render详解
2018/03/17 Javascript
详解JS函数stack size计算方法
2018/06/18 Javascript
AngularJS 事件发布机制
2018/08/28 Javascript
使用jQuery给Table动态增加行、清空table的方法
2018/09/05 jQuery
浅谈React碰到v-if
2018/11/04 Javascript
关于JavaScript 数组你应该知道的事情(推荐)
2019/04/10 Javascript
利用原生JS实现data方法示例代码
2019/05/28 Javascript
js实现3D旋转相册
2020/08/02 Javascript
微信小程序用户登录和登录态维护的实现
2020/12/10 Javascript
python调用cmd复制文件代码分享
2013/12/27 Python
将字典转换为DataFrame并进行频次统计的方法
2018/04/08 Python
python生成密码字典的方法
2018/07/06 Python
对Python3使运行暂停的方法详解
2019/02/18 Python
Python基于datetime或time模块分别获取当前时间戳的方法实例
2019/02/19 Python
python批量读取文件名并写入txt文件中
2020/09/05 Python
python模拟键盘输入 切换键盘布局过程解析
2019/08/15 Python
使用OpenCV实现仿射变换—平移功能
2019/08/29 Python
解决keras GAN训练是loss不发生变化,accuracy一直为0.5的问题
2020/07/02 Python
欧洲最大的笔和书写专家:The Pen Shop
2017/03/19 全球购物
洛杉矶生活休闲而精致的基础品牌:Mika Jaymes
2018/01/07 全球购物
澳洲健康食品网上商店:Aussie Health Products
2018/06/15 全球购物
捐款倡议书
2014/04/14 职场文书
党的群众教育实践活动实施方案
2014/06/12 职场文书
2015年医院药剂科工作总结
2015/05/04 职场文书
2016元旦晚会主持人开场白和结束语
2015/12/03 职场文书
微软团队与 NASA 科学家和惠普企业(HPE)的工程师合作
2022/04/21 数码科技