发布于 2014-08-17 15:06:30 | 250 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

CakePHP PHP开源框架

CakePHP是一个运用了诸如ActiveRecord、Association Data Mapping、Front Controller和MVC等著名设计模式的快速开发框架。该项目主要目标是提供一个可以让各种层次的PHP开发人员快速地开发出健壮的Web应用,而 又不失灵活性。


本文讲解了php开发框架cakephp的用法,CakePHP是一个运用了诸如ActiveRecord、Association Data Mapping、Front Controller和MVC等著名设计模式的快速开发框架。感兴趣的同学参考下.

CakePHP是一个运用了诸如ActiveRecord、Association Data Mapping、Front Controller和MVC等著名设计模式的快速开发框架。该项目主要目标是提供一个可以让各种层次的PHP开发人员快速地开发出健壮的Web应用,而 又不失灵活性。

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

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

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

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

代码:

<?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

    );
}

?>

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

增加Cotroller模块:

app/controllers/posts_controller.php

代码:

<?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

<h1>Blog posts</h1>
<p><?php echo $html->link("Add Post", "/posts/add"); ?>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Created</th>
    </tr>

   
<!-- Here's where we loop through our $posts array, printing out post info -->

    
<?php foreach ($posts as $post): ?>
    <tr>
        <td><?php echo $post['Post']['id']; ?></td>
        <td>
            <?php echo $html->link($post['Post']['title'], '/posts/view/'.$post['Post']['id']);?>
            <?php echo $html->link(
                'Delete',
                "/posts/delete/{$post['Post']['id']}",
                null,
                'Are you sure?'
            )
?>
            <?php echo $html->link('Edit', '/posts/edit/'.$post['Post']['id']);?>
        </td>
        </td>
        <td><?php echo $post['Post']['created']; ?></td>
    </tr>
    <?php endforeach; ?>

</table>

view.thtml

<h1><?php echo $post['Post']['title']?></h1>

<p><small>Created: <?php echo $post['Post']['created']?></small></p>

<p><?php echo $post['Post']['body']?></p>

<br>

<p><?php echo $html->link('Return', '/posts/index/');?></p>

edit.thtml

<h1>Edit Post</h1>
<form method="post" action="<?php echo $html->url('/posts/edit')?>">
    <?php echo $html->hidden('Post/id'); ?>
    <p>
        Title:
        
<?php echo $html->input('Post/title', array('size' => '40'))?>
        <?php echo $html->tagErrorMsg('Post/title', 'Title is required.') ?>
    </p>
    <p>
        Body:
        
<?php echo $html->textarea('Post/body', array('rows'=>'10')) ?>
        <?php echo $html->tagErrorMsg('Post/body', 'Body is required.') ?>
    </p>
    <p>
        <?php echo $html->submit('Save') ?>
    </p>
</form>

add.thtml

<h1>Add Post</h1>
<form method="post" action="<?php echo $html->url('/posts/add')?>">
    <p>
        Title:
        
<?php echo $html->input('Post/title', array('size' => '40'))?>
        <?php echo $html->tagErrorMsg('Post/title', 'Title is required.') ?>
    </p>
    <p>
        Body:
        
<?php echo $html->textarea('Post/body', array('rows'=>'10')) ?>
        <?php echo $html->tagErrorMsg('Post/body', 'Body is required.') ?>
    </p>
    <p>
        <?php echo $html->submit('Save') ?>
    </p>
</form>

测试URL: http://localhost/phpblog/posts/
参考网页:http://manual.cakephp.org/appendix/blog_tutorial



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务