Heim  >  Artikel  >  php教程  >  基于CakePHP实现的简单博客系统实例_php实例

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

PHP中文网
PHP中文网Original
2016-05-25 17:09:221099Durchsuche

这篇文章主要介绍了基于CakePHP实现的简单博客系统,以一个完整实例分析了使用CakePHP实现博客系统的完整流程,需要的朋友可以参考下

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

PostsController.php文件:

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

Post.php文件:

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

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://www.php.cn/)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://www.php.cn/)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright   Copyright 2005-2012, Cake Software Foundation, Inc. (http://www.php.cn/)
 * @link     http://www.php.cn/ CakePHP(tm) Project
 * @package    app.Config
 * @since     CakePHP(tm) v 0.2.9
 * @license    MIT License (http://www.php.cn/)
 */
/**
 * Here, we are connecting &#39;/&#39; (base path) to controller called &#39;Pages&#39;,
 * its action called &#39;display&#39;, and we pass a param to select the view file
 * to use (in this case, /app/View/Pages/home.ctp)...
 */
  //Router::connect(&#39;/&#39;, array(&#39;controller&#39; => &#39;pages&#39;, &#39;action&#39; => &#39;display&#39;, &#39;home&#39;));
  Router::connect(&#39;/&#39;, array(&#39;controller&#39; => &#39;posts&#39;, &#39;action&#39; => &#39;index&#39;));
/**
 * ...and connect the rest of &#39;Pages&#39; controller&#39;s urls.
 */
  Router::connect(&#39;/pages/*&#39;, array(&#39;controller&#39; => &#39;pages&#39;, &#39;action&#39; => &#39;display&#39;));
/**
 * 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 . &#39;Config&#39; . DS . &#39;routes.php&#39;;

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=&#39;+00:00&#39; */;
/*!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=&#39;NO_AUTO_VALUE_ON_ZERO&#39; */;
/*!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,&#39;The title&#39;,&#39;This is the post body.&#39;,&#39;2012-11-01 15:43:41&#39;,NULL),(2,&#39;A title once again&#39;,&#39;And the post body follows.&#39;,&#39;2012-11-01 15:43:41&#39;,NULL),(3,&#39;Title strikes back&#39;,&#39;This is really exciting! Not.&#39;,&#39;2012-11-01 15:43:41&#39;,NULL),(4,&#39;ggjjkhkhhk&#39;,&#39;7777777777777777777777777\r\n777777777777777777777777&#39;,&#39;2012-11-01 20:16:28&#39;,&#39;2012-11-01 20:16:28&#39;);
/*!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 (&#39;20121013024711&#39;),(&#39;20121013030850&#39;);
/*!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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php纯属疑问Nächster Artikel:一个简单的php数学运算验证码