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模式有了更深一步的理解。