基于CakePHP實(shí)現(xiàn)的簡(jiǎn)單博客系統(tǒng)實(shí)例
本文實(shí)例講述了基于CakePHP實(shí)現(xiàn)的簡(jiǎn)單博客系統(tǒng)。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- CakePHP框架Model關(guān)聯(lián)對(duì)象用法分析
- cakephp常見(jiàn)知識(shí)點(diǎn)匯總
- CakePHP框架Session設(shè)置方法分析
- cakephp2.X多表聯(lián)合查詢(xún)join及使用分頁(yè)查詢(xún)的方法
- Nginx配置PHP的Yii與CakePHP框架的rewrite規(guī)則示例
- cakephp打印sql語(yǔ)句的方法
- Cakephp 執(zhí)行主要流程
- 配置Apache2.2+PHP5+CakePHP1.2+MySQL5運(yùn)行環(huán)境
- CakePHP去除默認(rèn)顯示的標(biāo)題及圖標(biāo)的方法
- CakePHP框架Model函數(shù)定義方法示例
相關(guān)文章
基于thinkphp5框架實(shí)現(xiàn)微信小程序支付 退款 訂單查詢(xún) 退款查詢(xún)操作
這篇文章主要介紹了基于thinkphp5框架實(shí)現(xiàn)微信小程序支付 退款 訂單查詢(xún) 退款查詢(xún)操作,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08php 字符串中的\n換行符無(wú)效、不能換行的解決方法
這篇文章主要介紹了php 字符串中的換行符無(wú)效、不能換行的解決方法,實(shí)際上是PHP的雙引號(hào)和單引號(hào)的使用問(wèn)題,需要的朋友可以參考下2014-04-04Laravel 實(shí)現(xiàn)Eloquent模型分組查詢(xún)并返回每個(gè)分組的數(shù)量 groupBy()
今天小編就為大家分享一篇Laravel 實(shí)現(xiàn)Eloquent模型分組查詢(xún)并返回每個(gè)分組的數(shù)量 groupBy(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10用HTML/JS/PHP方式實(shí)現(xiàn)頁(yè)面延時(shí)跳轉(zhuǎn)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇用HTML/JS/PHP方式實(shí)現(xiàn)頁(yè)面延時(shí)跳轉(zhuǎn)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07php設(shè)計(jì)模式之職責(zé)鏈模式實(shí)例分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之職責(zé)鏈模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了php職責(zé)鏈模式相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03PHP+FastCGI+Nginx配置PHP運(yùn)行環(huán)境
Nginx不支持對(duì)外部程序的調(diào)用,所以必須通過(guò)FastCGI接口實(shí)現(xiàn)對(duì)外部程序的調(diào)用從而實(shí)現(xiàn)對(duì)客戶(hù)端動(dòng)態(tài)頁(yè)面請(qǐng)求的處理。2014-08-08