本篇文章给大家分享的是有关使用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 -- -- 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; -- Dump completed on 2012-11-01 16:41:46
以上就是使用CakePHP怎么编写一个博客系统,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。