欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-22 20:31:37
本文实例讲述了基于cakephp实现的简单博客系统。分享给大家供大家参考。具体实现方法如下: postscontroller.php文件:

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

postscontroller.php文件:

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

<?php
class 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';

blog.sql文件如下:

-- mysql dump 10.13 distrib 5.5.19, for win64 (x86)
--
-- host: localhost  database: facebook
-- ------------------------------------------------------
-- server version  5.5.19
/*!40101 set @old_character_set_client=@@character_set_client */;
/*!40101 set @old_character_set_results=@@character_set_results */;
/*!40101 set @old_collation_connection=@@collation_connection */;
/*!40101 set names utf8 */;
/*!40103 set @old_time_zone=@@time_zone */;
/*!40103 set time_zone='+00:00' */;
/*!40014 set @old_unique_checks=@@unique_checks, unique_checks=0 */;
/*!40014 set @old_foreign_key_checks=@@foreign_key_checks, foreign_key_checks=0 */;
/*!40101 set @old_sql_mode=@@sql_mode, sql_mode='no_auto_value_on_zero' */;
/*!40111 set @old_sql_notes=@@sql_notes, sql_notes=0 */;
 
--
-- table structure for table `posts`
--
drop table if exists `posts`;
/*!40101 set @saved_cs_client   = @@character_set_client */;
/*!40101 set character_set_client = utf8 */;
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;
/*!40101 set character_set_client = @saved_cs_client */;
--
-- dumping data for table `posts`
--
lock tables `posts` write;
/*!40000 alter table `posts` disable keys */;
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','7777777777777777777777777\r\n777777777777777777777777','2012-11-01 20:16:28','2012-11-01 20:16:28');
/*!40000 alter table `posts` enable keys */;
unlock tables;
--
-- table structure for table `schema_migrations`
--
drop table if exists `schema_migrations`;
/*!40101 set @saved_cs_client   = @@character_set_client */;
/*!40101 set character_set_client = utf8 */;
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;
/*!40101 set character_set_client = @saved_cs_client */;
--
-- dumping data for table `schema_migrations`
--
lock tables `schema_migrations` write;
/*!40000 alter table `schema_migrations` disable keys */;
insert into `schema_migrations` values ('20121013024711'),('20121013030850');
/*!40000 alter table `schema_migrations` enable keys */;
unlock tables;
/*!40103 set time_zone=@old_time_zone */;
/*!40101 set sql_mode=@old_sql_mode */;
/*!40014 set foreign_key_checks=@old_foreign_key_checks */;
/*!40014 set unique_checks=@old_unique_checks */;
/*!40101 set character_set_client=@old_character_set_client */;
/*!40101 set character_set_results=@old_character_set_results */;
/*!40101 set collation_connection=@old_collation_connection */;
/*!40111 set sql_notes=@old_sql_notes */;
-- dump completed on 2012-11-01 16:41:46

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