基于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 数组排序 array_multisort与uasort的区别
Mar 24 PHP
一致性哈希算法以及其PHP实现详细解析
Aug 24 PHP
php打开远程文件的方法和风险及解决方法
Nov 12 PHP
PHP使用正则表达式清除超链接文本
Nov 12 PHP
phpword插件导出word文件时中文乱码问题处理方案
Aug 19 PHP
PHP实现图片裁剪、添加水印效果代码
Oct 01 PHP
php简单实现无限分类树形列表的方法
Mar 27 PHP
php判断对象是派生自哪个类的方法
Jun 20 PHP
PHP中set error handler函数用法小结
Nov 11 PHP
Yii2实现UploadedFile上传文件示例
Feb 15 PHP
PHP插件PHPMailer发送邮件功能
Feb 28 PHP
PHP闭包定义与使用简单示例
Apr 13 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模块部分功能的简单封装
2011/09/30 PHP
PHP也能干大事之PHP中的编码解码详解
2015/04/20 PHP
PHP基于单例模式实现的数据库操作基类
2016/01/15 PHP
功能强大的php文件上传类
2016/08/29 PHP
PHP实现对xml的增删改查操作案例分析
2017/05/19 PHP
TP5框架实现自定义分页样式的方法示例
2020/04/05 PHP
JavaScript实现公历转农历功能示例
2017/02/13 Javascript
jQuery.Form实现Ajax上传文件同时设置headers的方法
2017/06/26 jQuery
vue数字类型过滤器的示例代码
2017/09/07 Javascript
原生JS实现的多个彩色小球跟随鼠标移动动画效果示例
2018/02/01 Javascript
解决angular2 获取到的数据无法实时更新的问题
2018/08/31 Javascript
详解Vue 如何监听Array的变化
2019/06/06 Javascript
从零搭一个自用的前端脚手架的方法步骤
2019/09/23 Javascript
vue实现配置全局访问路径头(axios)
2019/11/01 Javascript
微信小程序实现左滑删除效果
2020/11/18 Javascript
Python Pandas找到缺失值的位置方法
2018/04/12 Python
详解python 注释、变量、类型
2018/08/10 Python
Python实现最大子序和的方法示例
2019/07/05 Python
Python TCPServer 多线程多客户端通信的实现
2019/12/31 Python
python实现快递价格查询系统
2020/03/03 Python
学习Python列表的基础知识汇总
2020/03/10 Python
python和js交互调用的方法
2020/06/23 Python
8款精美的CSS3表单设计(登录表单/下拉选择/按钮附演示及源码)
2013/02/04 HTML / CSS
大专自我鉴定范文
2013/10/01 职场文书
土木工程毕业生自荐信
2013/11/12 职场文书
教育技术职业规划范文
2014/03/04 职场文书
艺术节主持词
2014/04/02 职场文书
赡养老人协议书
2014/04/21 职场文书
房地产推广策划方案
2014/05/19 职场文书
物业公司的岗位任命书
2014/06/06 职场文书
社区安全生产月活动总结
2014/07/05 职场文书
授权委托书
2014/07/31 职场文书
机关作风建设整改方案
2014/10/27 职场文书
杨善洲电影观后感
2015/06/04 职场文书
Python实现提取PDF简历信息并存入Excel
2022/04/02 Python
设置IIS Express并发数
2022/07/07 Servers