PHP框架之CakePHP学习

1. CakePHP的安装和配置

      1)在http://cakephp.org/下载CakePHP的安装包.

    我是在xampp环境下配置的,将安装包解压到自己的DocumentRoot路径下,即自己的xampp\htdocs路径。我放在了我的编辑器Dreamweaver的站点之下,D:\xampp\htdocs\Zhandian\CakePHP

    2)安装DebugKit

       下载解压到CakePHP\Plugin\DebugKit

       然后修改CakePHP\app\Config\bootstrap.php文件,去掉下列两行代码前面的注释,让DebugKit加载进来

     CakePlugin::loadAll(); // Loads all plugins at once   
     CakePlugin::load('DebugKit'); //Loads a single plugin named DebugKit
       保存。然后修改CakePHP\app\Controll.php文件

    class AppController extends Controller {  
         public $components = array('DebugKit.Toolbar');  
    } 
        保存,然后修改CakePHP\app\Config\core.php,找到Configure::write('debug',2); 将2改为1,即改为开发级别

   

       Configure::write('debug', 1); 

    在浏览器输入http://localhost:8081/Zhandian/CakePHP,会发现浏览器提示有很多错误和警告。按照提示修改就好,各种错误都可以在网上查找到。

     参考链接:http://blog.sina.com.cn/s/blog_6210c3e2010117xu.html

     最终配置成功后的结果


2. 创建一个blog

     参考链接:http://blog.csdn.net/wjazz/article/details/2622976

    1)建立数据库

/* 首先建立表: */  
CREATE TABLE posts (  
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,  
title VARCHAR(50),  
text TEXT,  
modified DATETIME DEFAULT NULL  
);  

/* 添加一些简单文章 */  
INSERT INTO posts (title,text , modified)  
VALUES ('The title', 'This is the post body.', NOW());  
INSERT INTO posts (title, text, modified)  
VALUES ('A title once again', 'And the post body follows.', NOW());  
INSERT INTO posts (title, text, modified)  
VALUES ('Title strikes back', 'This is really exciting! Not.', NOW()); 
    2)建立模型文件

     在CakePHP\app\Model目录下建立post.php文件

<?php  
class Post extends AppModel {  
     public $validate = array(
        'title' => array(
            'rule' => 'notBlank'
        ),
        'text' => array(
            'rule' => 'notBlank'
        )
    );    
}  
?>  

   3)建立控制器

      在CakePHP\app\Controller目录下创建PostsController.php

<?php
class PostsController extends AppController {
    public $helpers = array('Html', 'Form');

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

    public function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }

        $post = $this->Post->findById($id);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));
        }
        $this->set('post', $post);
    }
public function add() {
        if ($this->request->is('post')) {
            $this->Post->create();
            if ($this->Post->save($this->request->data)) {
                $this->flash('Your post has been updated.','/posts');
                return $this->redirect(array('action' => 'index'));
            }
           $this->flash('Unable to update your post.','/posts');
        }
    }
public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid post'));
    }

    $post = $this->Post->findById($id);
    if (!$post) {
        throw new NotFoundException(__('Invalid post'));
    }

    if ($this->request->is(array('post', 'put'))) {
        $this->Post->id = $id;
        if ($this->Post->save($this->request->data)) {
            $this->flash('Your post has been updated.','/posts');
            return $this->redirect(array('action' => 'index'));
        }
        $this->flash('Unable to update your post.','/posts');
    }

    if (!$this->request->data) {
        $this->request->data = $post;
    }
}
public function delete($id) {
    if ($this->request->is('get')) {
        throw new MethodNotAllowedException();
    }

    if ($this->Post->delete($id)) {
        $this->flash('The post with id: %s has been deleted.', h($id));
    } else {
        $this->flash('The post with id: %s could not be deleted.', h($id));
    }

    return $this->redirect(array('action' => 'index'));
}
}
?>

    4)创建视图

      在CakePHP\app\View\Posts下创建4个文件:index.ctp, view.ctp, add.ctp, edit.ctp

      index.ctp文件:

<h1>Blog posts</h1>
<p><?php echo $this->Html->link('Add Post', array('action' => 'add')); ?></p>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Actions</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 $this->Html->link(
                    $post['Post']['title'],
                    array('action' => 'view', $post['Post']['id'])
                );
            ?>
        </td>
        <td>
            <?php
                echo $this->Form->postLink(
                    'Delete',
                    array('action' => 'delete', $post['Post']['id']),
                    array('confirm' => 'Are you sure?')
                );
            ?>
            <?php
                echo $this->Html->link(
                    'Edit', array('action' => 'edit', $post['Post']['id'])
                );
            ?>
        </td>
        <td>
            <?php echo $post['Post']['modified']; ?>
        </td>
    </tr>
    <?php endforeach; ?>

</table>

     view.ctp文件:

<h1><?php echo h($post['Post']['title']); ?></h1>

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

<p><?php echo h($post['Post']['text']); ?></p>

     add.ctp文件:

<h1>Add Post</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('text', array('rows' => '3'));
echo $this->Form->end('Save Post');
?>

    edit.ctp文件:

<h1>Edit Post</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('text', array('rows' => '3'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Post');
?>

  

    好了,模型层,视图层,控制层的内容都写完了,可以看看最终结果了,哇哈哈~现在可以对其中的文章进行增删改。

     增加一个My blog




   

     删除Delete test


 

 4.总结:通过这个简单blog的制作,对MVC模式有了更深一步的理解。

  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值