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

本文实例讲述了基于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程序设计有所帮助。

(0)

相关推荐

  • Cakephp 执行主要流程

    加载基本文件 cake/basics.php 里面定义了常用的方法以及时间常量 $TIME_START = getMicrotime(); 记录开始执行时间 cake/config/paths.php 里面定义一些基本路径 cake/lib/object.php cake的基本类 cake/lib/inflector.php 这里主要是处理单复数,带下划开命名以及驼峰式命名 cake/lib/configure.php 里面提供文件配置的读写,路径的设置,以及加载文件的方法 cake/lib/c

  • Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例

    Yii的Nginx rewrite 如下为nginx yii的重写 server { set $host_path "/data/site/www.jb51.net"; access_log /data/logs/nginx/www.jb51.net_access.log main; server_name jb51.net www.jb51.net; root $host_path/htdocs; set $yii_bootstrap "index.php"; #

  • cakephp常见知识点汇总

    本文实例总结了cakephp常见知识点.分享给大家供大家参考,具体如下: 1. 调用其他控制器的模板,重定向 方法一: 在此调用/views/tasks/tasks下的hello.ctp模板 $this -> viewPath = 'tasks'; $this -> render('hello'); 方法二(带参): $this->redirect(array('controller'=>'users','action'=>'welcome',urlencode($this-

  • CakePHP框架Model函数定义方法示例

    本文实例讲述了CakePHP框架Model函数定义方法.分享给大家供大家参考,具体如下: 在CakePHP中,MVC的架构是清晰的,而在实际做项目中,我发现仍然有很多人喜欢在Controller中堆砌函数,这样做也未尝不可,但是,作为一个百万行级的大项目来说,这种违背MVC思想的做法虽然可能暂时给程序结构带来便利,但从长远来看,是万万不可取的! 我们应该将系统常用到的某些函数定义在Model中,特别是纯粹的的数据处理函数和数据查询函数: 譬如,在Blog中像下面这样的条件查询: /* * * B

  • cakephp打印sql语句的方法

    本文实例讲述了cakephp打印sql语句的方法.分享给大家供大家参考.具体实现方法如下: 将以下语句复制到你的代码中,可以打印出在这之前所有的sql语句: $sources = ConnectionManager::sourceList(); if (!isset($logs)): $logs = array(); foreach ($sources as $source): $db =& ConnectionManager::getDataSource($source); if (!$db-

  • CakePHP框架Model关联对象用法分析

    本文实例讲述了CakePHP框架Model关联对象.分享给大家供大家参考,具体如下: CakePHP 提供关联数据表间的映射,共有4种类型的关联: hasOne,hasMany,belongTo,hasAndBelongsToMany. 设定了Model间的关联关系定义,CakePHP就会将基于关系数据库的数据映射为基于对象的关系模型. 但是你应该确保遵循CakePHP的命名规则. 命名规则中需要考虑的3个内容是,外键,model名字,表名. 外键:单数形式的 modelName_id 表名:复

  • cakephp2.X多表联合查询join及使用分页查询的方法

    本文实例讲述了cakephp2.X多表联合查询join及使用分页查询的方法.分享给大家供大家参考,具体如下: 格式化参数: public function getconditions($data){ $this->loadModel("Cm.LoginHistory"); $conditions = array(); foreach ($data as $key=>$val){ if($key=='start_date'){ $conditions['LoginHistor

  • 配置Apache2.2+PHP5+CakePHP1.2+MySQL5运行环境

    1. 安装配置Apahce 安装配置Apache是比较简单的, 跟着安装向导一步步往下走就能搞定.最多就是在配置端口的地方需要注意一下,如果已经安装了其它Web服务器占用了80端口,那记得配置的时候选一个别的端口.向导中忘了设置,在Apache的conf/httpd.conf中修改下面这句就好: Listen 127.0.0.1:80 2. 安装配置PHP5 PHP5也是一路安装就完了.要让Apache能解释PHP页面,继续修改Apache的conf/httpd.conf文件. 首先,假设PHP

  • CakePHP去除默认显示的标题及图标的方法

    去除的办法是: 修改cake\libs\view\templates\layouts\default.thtml,这个是视图文件的通用模板框架(带头部和脚部). 另外: cakephp视图文件的扩展名都是thtml,这个是默认值,如果想修改后缀名也是可以的. 修改的方法是: 早cake\libs\controller\controller.php里把 var $ext='.thtml'改成html即可.

  • CakePHP框架Session设置方法分析

    本文实例讲述了CakePHP框架Session设置方法.分享给大家供大家参考,具体如下: CakePHP Session 存储选项 CakePHP提供给用户了3种Session数据保存方式: 1. CakePHP安装目下的临时文件; 2. 采用PHP的默认机制; 3. 或者序列化到数据库中. 对应的设置在core.php中分别为: define('CAKE_SESSION_SAVE', 'php'); # 设置为 'cake',保存session到 /cakedistro/tmp目录 # 设置为

随机推荐