In this paper, a simple blog system based on cakephp implementation is described. Share to everyone for your reference. The implementation method is as follows:
postscontroller.php file:
<?phpclass Postscontroller extends AppController {public $helpers = array (' Html ', ' Form ', ' Session '); public $componen ts = 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 file:
<?phpclass Post extends Appmodel {public $validate = array (' title ' = = Array (' rule ' = ' notempty '), ' body ' = = Array (' rule ' = ' notempty ');}? >
routes.php file:
<?php/** * Routes Configuration * * In the This file, you set up Routes to your controllers and their actions. * Routes is very important mechanism that allows the freely connect * different URLs to chosen controllers and their A Ctions (Functions). * PHP 5 * * CakePHP (TM): Rapid development Framework (http://cakephp.org) * Copyright 2005-2012, Cake software Foundati On, Inc. (http://cakefoundation.org) * * Licensed under the MIT License * redistributions of files must retain the above C Opyright notice. * * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) * @link Http://cakeph p.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're connecting '/' (base path) to controller called ' Pages ', * Its action called ' Display ', and we pass a PA RAM to select the view file * To use (in this case,/APP/VIEW/PAGES/HOME.CTP)... *///router::connect ('/', Array (' controller ' = ' ['] ' ['] ', ' action ' = ' display ', ' home ')); Router::connect ('/', Array (' controller ' = ' posts ', ' action ' = ' index ');/** * ... and connect the rest of ' Pages ' C Ontroller ' s URLs. */Router::connect ('/pages/* ', Array (' controller ' = ' ' pages ', ' action ' = ' display '),/** * Load all plugin routes. See the Cakeplugin documentation in * How to customize the loading of plugin routes. */Cakeplugin::routes ();/** * Load the CakePHP default routes. Only remove the built-in default routes for want to use *. */Require CAKE. ' Config '. Ds. ' routes.php ';
The
Blog.sql file is as follows:
--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_cli ent */;/*!40101 SET character_set_client = UTF8 */; CREATE TABLE ' posts ' (' id ' int (ten) unsigned not NULL auto_increment, ' title ' varchar (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=u tf8_unicode_ci;/*!40101 SET character_set_client = @saved_cs_client */;----dumping data for table ' posts '--lock TABLES ' p OSTs ' write;/*!40000 ALTER TABLE ' posts ' DISABLE KEYS */;insert into ' posts ' VALUES (1, ' The title ', ' This is the ' 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 @s Aved_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_migr Ations ' (' 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
I hope this article is helpful to everyone's PHP programming.