本文实例讲述了基于CakePHP实现的简单博客系统。分享给大家供大家参考。具体实现方法如下:
PostsController.php文件:
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文件:
array( 'rule' => 'notEmpty' ), 'body' => array( 'rule' => 'notEmpty' ) ); } ?>
routes.php文件:
'pages', 'action' => 'display', 'home'));
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
Router::connect('/pages
CakePlugin::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`;
;
;
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;
;
--
-- Dumping data for table `posts`
--
LOCK TABLES `posts` WRITE;
;
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','7777777777777777777777777rn777777777777777777777777','2012-11-01 20:16:28','2012-11-01 20:16:28');
;
UNLOCK TABLES;
--
-- Table structure for table `schema_migrations`
--
DROP TABLE IF EXISTS `schema_migrations`;
;
;
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;
;
--
-- Dumping data for table `schema_migrations`
--
LOCK TABLES `schema_migrations` WRITE;
;
INSERT INTO `schema_migrations` VALUES ('20121013024711'),('20121013030850');
;
UNLOCK TABLES;
;
;
;
;
;
;
;
;
-- Dump completed on 2012-11-01 16:41:46
希望本文所述对大家的php程序设计有所帮助。



