基于CakePHP实现的简单博客系统实例

时间:2021-05-25

本文实例讲述了基于CakePHP实现的简单博客系统。分享给大家供大家参考。具体实现方法如下:

PostsController.php文件:

<?phpclass 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文件:

<?phpclass 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://pleted on 2012-11-01 16:41:46

希望本文所述对大家的php程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章