本文实例讲述了基于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://www.opensource.org/licenses/mit-license.php) *//** * Here, we are connecting '/' (base path) to controller called 'Pages', * its action called 'display', and we pass a param to select the view file * to use (in this case, /app/View/Pages/home.ctp)... */ //Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); Router::connect('/', array('controller' => 'posts', 'action' => 'index'));/** * ...and connect the rest of 'Pages' controller's urls. */ Router::connect('/pages/*', array('controller' => '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';
新闻热点
疑难解答