基于CakePHP实现的简单博客系统实例
本篇章节讲解基于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'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/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文件如下: --
-- Table structure for table posts
DROP TABLE IF EXISTS
|