Home > Backend Development > PHP Tutorial > 攻克CakePHP(PHP中的Ruby On Rails框架)图文介绍_php技巧

攻克CakePHP(PHP中的Ruby On Rails框架)图文介绍_php技巧

PHP中文网
Release: 2016-05-17 09:34:45
Original
1176 people have browsed it

CakePHP框架实例介绍分析。图文并茂

CakePHP框架首页: http://www.cakephp.org/

下载后导入工程中,目录结构如下图(使用版本:1.1.19.6305)

搭建PHP环境,这里使用了AppServ2.5.9。 下载主页 http://www.php.cn/

MySQL中新建数据库blog,并运行如下SQL文建表。

/* First, create our posts table: */
CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

/* Then insert some posts for testing: */
INSERT INTO posts (title,body,created)
    VALUES ('The title', 'This is the post body.', NOW());
INSERT INTO posts (title,body,created)
    VALUES ('A title once again', 'And the post body follows.', NOW());
INSERT INTO posts (title,body,created)
    VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());

修改工程app/config/目录下database.php.default文件名为database.php,并修改其配置。

修改Apache的httpd.conf文件。

Apache2.2版本的,直接把#LoadModule rewrite_modulemodules/mod_rewrite.so的注释删掉即可。

2.0以前的版本,据说要修改2个地方: LoadModule rewrite_module libexec/httpd/mod_rewrite.soAddModule mod_rewrite.c

增加Model:

/app/models/post.php

代码:


require_once ('cake/app_model.php');

class Post extends AppModel {

public $name = 'Post';

public $validate = array(

'title' => VALID_NOT_EMPTY,
        'body'   => VALID_NOT_EMPTY

    );
}

?>

增加Cotroller模块:

app/controllers/posts_controller.php

代码:


require_once ('cake/app_controller.php');

class PostsController extends AppController {

public $name = 'Posts';

function index()
{
$this->set('posts', $this->Post->findAll());
    }

    function view($id = null)
    {
        $this->Post->id = $id;
        $this->set('post', $this->Post->read());
    }
    
    function add()
    {
        if (!empty($this->data))
        {
            if ($this->Post->save($this->data))
            {
                //$this->flash('Your post has been saved.','/posts');
                $this->redirect("/posts/index");
            }
        }
    }
    
    function delete($id)
    {
        $this->Post->del($id);
        //$this->flash('The post with id: '.$id.' has been deleted.', '/posts');
        $this->redirect("/posts/index");
    }
    
    function edit($id = null)
    {
        if (empty($this->data))
        {
            $this->Post->id = $id;
            $this->data = $this->Post->read();
        }
        else
        {
            if ($this->Post->save($this->data['Post']))
            {
                //$this->flash('Your post has been updated.','/posts');
                $this->redirect("/posts/index");
            }
        }
    }
}

?>

增加页面模块:

/app/views/下追加posts文件夹,然后再添加4个页面(一般后缀为.rhtml)

代码依次为:

index.thtml

Blog posts


link("Add Post", "/posts/add"); ?>


    
        
        
        
    

   

    
    
        
        
        
        
    
    

IdTitleCreated

            link($post['Post']['title'], '/posts/view/'.$post['Post']['id']);?>
            link(
                'Delete',
                "/posts/delete/{$post['Post']['id']}",
                null,
                'Are you sure?'
            )?>
            link('Edit', '/posts/edit/'.$post['Post']['id']);?>
        

view.thtml



Created: 








link('Return', '/posts/index/');?>

edit.thtml

Edit Post


url('/posts/edit')?>">
    hidden('Post/id'); ?>
    


        Title:
        input('Post/title', array('size' => '40'))?>
        tagErrorMsg('Post/title', 'Title is required.') ?>
    


    


        Body:
        textarea('Post/body', array('rows'=>'10')) ?>
        tagErrorMsg('Post/body', 'Body is required.') ?>
    


    


        submit('Save') ?>
    


add.thtml

Add Post


url('/posts/add')?>">
    


        Title:
        input('Post/title', array('size' => '40'))?>
        tagErrorMsg('Post/title', 'Title is required.') ?>
    


    


        Body:
        textarea('Post/body', array('rows'=>'10')) ?>
        tagErrorMsg('Post/body', 'Body is required.') ?>
    


    


        submit('Save') ?>
    


测试URL: http://localhost/phpblog/posts/
参考网页:http://manual.cakephp.org/appendix/blog_tutorial
以下是CakePHP框架的一些特性: http://www.lostk.com/blog/cakephp_note/

e="COLOR: #000000">
        Title:
        input('Post/title', array('size' => '40'))?>
        tagErrorMsg('Post/title', 'Title is required.') ?>
    


    


        Body:
        textarea('Post/body', array('rows'=>'10')) ?>
        tagErrorMsg('Post/body', 'Body is required.') ?>
    


    


        submit('Save') ?>
    



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template