基于CakePHP实现的简单博客系统实例
本文实例讲述了基于CakePHP实现的简单博客系统。分享给大家供大家参考。具体实现方法如下:
PostsController.php文件:
<?php
classPostsControllerextendsAppController{
public$helpers=array('Html','Form','Session');
public$components=array('Session');
publicfunctionindex()
{
$this->set('posts',$this->Post->find('all'));
}
publicfunctionview($id=null)
{
$this->Post->id=$id;
$this->set('post',$this->Post->read());
}
publicfunctionadd()
{
if($this->request->is("post"))
{
$this->Post->create();
if($this->Post->save($this->request->data))
{
$this->Session->setFlash("yourpostadded!");
$this->redirect(array('action'=>'index'));
}
else
{
$this->Session->setFlash("unabletocreatepost!");
}
}
}
publicfunctionedit($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('Yourposthasbeenupdated.');
$this->redirect(array('action'=>'index'));
}
else
{
$this->Session->setFlash('Unabletoupdateyourpost.');
}
}
}
publicfunctiondelete($id){
if($this->request->is('get')){
thrownewMethodNotAllowedException();
}
if($this->Post->delete($id)){
$this->Session->setFlash('Thepostwithid:'.$id.'hasbeendeleted.');
$this->redirect(array('action'=>'index'));
}
}
}
?>
Post.php文件:
<?php
classPostextendsAppModel{
public$validate=array(
'title'=>array(
'rule'=>'notEmpty'
),
'body'=>array(
'rule'=>'notEmpty'
)
);
}
?>
routes.php文件:
<?php
/**
*Routesconfiguration
*
*Inthisfile,yousetuproutestoyourcontrollersandtheiractions.
*Routesareveryimportantmechanismthatallowsyoutofreelyconnect
*differenturlstochosencontrollersandtheiractions(functions).
*
*PHP5
*
*CakePHP(tm):RapidDevelopmentFramework(http://cakephp.org)
*Copyright2005-2012,CakeSoftwareFoundation,Inc.(http://cakefoundation.org)
*
*LicensedunderTheMITLicense
*Redistributionsoffilesmustretaintheabovecopyrightnotice.
*
*@copyrightCopyright2005-2012,CakeSoftwareFoundation,Inc.(http://cakefoundation.org)
*@linkhttp://cakephp.orgCakePHP(tm)Project
*@packageapp.Config
*@sinceCakePHP(tm)v0.2.9
*@licenseMITLicense(http://www.opensource.org/licenses/mit-license.php)
*/
/**
*Here,weareconnecting'/'(basepath)tocontrollercalled'Pages',
*itsactioncalled'display',andwepassaparamtoselecttheviewfile
*touse(inthiscase,/app/View/Pages/home.ctp)...
*/
//Router::connect('/',array('controller'=>'pages','action'=>'display','home'));
Router::connect('/',array('controller'=>'posts','action'=>'index'));
/**
*...andconnecttherestof'Pages'controller'surls.
*/
Router::connect('/pages/*',array('controller'=>'pages','action'=>'display'));
/**
*Loadallpluginroutes.SeetheCakePlugindocumentationon
*howtocustomizetheloadingofpluginroutes.
*/
CakePlugin::routes();
/**
*LoadtheCakePHPdefaultroutes.Onlyremovethisifyoudonotwanttouse
*thebuilt-indefaultroutes.
*/
requireCAKE.'Config'.DS.'routes.php';
blog.sql文件如下:
--MySQLdump10.13Distrib5.5.19,forWin64(x86)
--
--Host:localhostDatabase:facebook
--------------------------------------------------------
--Serverversion5.5.19
/*!40101SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;
/*!40101SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;
/*!40101SET@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;
/*!40101SETNAMESutf8*/;
/*!40103SET@OLD_TIME_ZONE=@@TIME_ZONE*/;
/*!40103SETTIME_ZONE='+00:00'*/;
/*!40014SET@OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS,UNIQUE_CHECKS=0*/;
/*!40014SET@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0*/;
/*!40101SET@OLD_SQL_MODE=@@SQL_MODE,SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/;
/*!40111SET@OLD_SQL_NOTES=@@SQL_NOTES,SQL_NOTES=0*/;
--
--Tablestructurefortable`posts`
--
DROPTABLEIFEXISTS`posts`;
/*!40101SET@saved_cs_client=@@character_set_client*/;
/*!40101SETcharacter_set_client=utf8*/;
CREATETABLE`posts`(
`id`int(10)unsignedNOTNULLAUTO_INCREMENT,
`title`varchar(50)COLLATEutf8_unicode_ciDEFAULTNULL,
`body`textCOLLATEutf8_unicode_ci,
`created`datetimeDEFAULTNULL,
`modified`datetimeDEFAULTNULL,
PRIMARYKEY(`id`)
)ENGINE=InnoDBAUTO_INCREMENT=5DEFAULTCHARSET=utf8COLLATE=utf8_unicode_ci;
/*!40101SETcharacter_set_client=@saved_cs_client*/;
--
--Dumpingdatafortable`posts`
--
LOCKTABLES`posts`WRITE;
/*!40000ALTERTABLE`posts`DISABLEKEYS*/;
INSERTINTO`posts`VALUES(1,'Thetitle','Thisisthepostbody.','2012-11-0115:43:41',NULL),(2,'Atitleonceagain','Andthepostbodyfollows.','2012-11-0115:43:41',NULL),(3,'Titlestrikesback','Thisisreallyexciting!Not.','2012-11-0115:43:41',NULL),(4,'ggjjkhkhhk','7777777777777777777777777\r\n777777777777777777777777','2012-11-0120:16:28','2012-11-0120:16:28');
/*!40000ALTERTABLE`posts`ENABLEKEYS*/;
UNLOCKTABLES;
--
--Tablestructurefortable`schema_migrations`
--
DROPTABLEIFEXISTS`schema_migrations`;
/*!40101SET@saved_cs_client=@@character_set_client*/;
/*!40101SETcharacter_set_client=utf8*/;
CREATETABLE`schema_migrations`(
`version`varchar(255)COLLATEutf8_unicode_ciNOTNULL,
UNIQUEKEY`unique_schema_migrations`(`version`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8COLLATE=utf8_unicode_ci;
/*!40101SETcharacter_set_client=@saved_cs_client*/;
--
--Dumpingdatafortable`schema_migrations`
--
LOCKTABLES`schema_migrations`WRITE;
/*!40000ALTERTABLE`schema_migrations`DISABLEKEYS*/;
INSERTINTO`schema_migrations`VALUES('20121013024711'),('20121013030850');
/*!40000ALTERTABLE`schema_migrations`ENABLEKEYS*/;
UNLOCKTABLES;
/*!40103SETTIME_ZONE=@OLD_TIME_ZONE*/;
/*!40101SETSQL_MODE=@OLD_SQL_MODE*/;
/*!40014SETFOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS*/;
/*!40014SETUNIQUE_CHECKS=@OLD_UNIQUE_CHECKS*/;
/*!40101SETCHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT*/;
/*!40101SETCHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS*/;
/*!40101SETCOLLATION_CONNECTION=@OLD_COLLATION_CONNECTION*/;
/*!40111SETSQL_NOTES=@OLD_SQL_NOTES*/;
--Dumpcompletedon2012-11-0116:41:46
希望本文所述对大家的php程序设计有所帮助。