tutoria mvc framework

8
Tutorial : Xây Dựng MVC Framework Cơ bản với PHP Writer : Lưu Xuân Trường I. Giới thiệu: II. Mô hình xử lý của MVC framework: III. Xây dựng cấu trúc folder: IV. Ví dụ cơ bản: (book manager) V. Mở rộng: I. Giới thiệu: - MVC parttern chia ứng dụng của bạn làm 3 phần: + Model: chịu trách nhiệm quản lý dữ liệu, mọi tác vụ tương tác với cơ sở dữ liệu của bạn đều nằm ở đây. + View: chịu trách nhiệm hiển thị dữ liệu được cung cấp bởi model. + Controller: điều khiển view và model làm việc với nhau. Controller nhận request từ client, gọi model thực thi tương ứng, và gởi trả dữ liệu lại cho client bằng view, view định dạng dữ liệu để hiển thị cho người dùng. II. Mô hình xử lý của MVC framework: Controller Model View Client Request Data Object Return Data Http Request Http Response

Upload: tien-le

Post on 07-Nov-2014

905 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Tutoria mvc framework

Tutorial : Xây Dựng MVC Framework Cơ bản với PHP

Writer : Lưu Xuân Trường

I. Giới thiệu:II. Mô hình xử lý của MVC framework:III. Xây dựng cấu trúc folder:IV. Ví dụ cơ bản: (book manager)V. Mở rộng:

I. Giới thiệu:

- MVC parttern chia ứng dụng của bạn làm 3 phần:

+ Model: chịu trách nhiệm quản lý dữ liệu, mọi tác vụ tương tác với cơ sở dữ liệu của bạn đều nằm ở đây.

+ View: chịu trách nhiệm hiển thị dữ liệu được cung cấp bởi model.

+ Controller: điều khiển view và model làm việc với nhau. Controller nhận request từ client, gọi model thực thi tương ứng, và gởi trả dữ liệu lại cho client bằng view, view định dạng dữ liệu để hiển thị cho người dùng.

II. Mô hình xử lý của MVC framework:

Controller Model

View

Client

Request Data Object

Return Data

Http Request

Http Response

Page 2: Tutoria mvc framework

Giải thích quy trình:

Người dùng (client) dùng web browser, gởi request đến Server, mọi request gởi đến Server đều được điều hướng đến file index.php. Tại index.php chúng ta thực thi Controller, Controller có nhiệm vụ bắt request, phân tích request để gọi Model xử lý và đổ dữ liệu ra View tương ứng. Sau đó hiển thị kết quả View ra trình duyệt cho người dùng.

Ví dụ http request:

- http request dạng chưa rewrite: http://localhost/index.php?controller=book&action=list- http request dạng đã rewrite: http://localhost/book/list (để rewrite url ta dùng htaccess).

III. Xây dựng cấu trúc folder: - Các bạn xây dựng cấu trúc như hình vẽ (cấu trúc đơn giản cơ bản), hoặc có theo cấu trúc

khác mà các bạn cho là dễ dàng thao tác và quy ước theo các bạn.

- Hoặc có thể tham khảo cấu trúc folder của Codeigniter Framework. (đã hiểu cấu trúc cơ bản ở trên ^^)

IV. Ví dụ cơ bản: (Book manager):

1. Xây dựng Model:

//file book_model.php

Class Book_Model

{

Page 3: Tutoria mvc framework

Public $dataTemp = array(

1 => array(

“title” => “Yii framerk”,

“description” => “Yii framework description”,

“price” => 45.5

),

2 => array(

“title” => “javascript ahead”,

“description” => “javascript ahead description”,

“price” => 15.8

),

3 => array(

“title” => “PHP Beginner”,

“descrition” => “PHP Beginner description”,

“price” => 55.55

)

);

Public function getListBooks()

{

Return $this->dataTemp;

}

Public function getBook($id)

{

Return $this->dataTemp[$id];

}}

2. Xây dụng Controller:

//file book_controller.php

Nội dung của trang controller.php

Include “model/book_model.php”;

Class Book_Controller

Page 4: Tutoria mvc framework

{

Public $model;

Public function __constructor()

{

$this->model = new Book_Model();

}

Public function invoke()

{

Switch($atc)

{

Case “view” :

$id = $_GET[“id”] || 0;

$this->view($id);break;

Case “list”:

Default:

$this->list();break;

}

}

Public function list()

{

$listBooks = $this->model->getListBooks();

Include “view/booklist.php”;

}

Public function view($id)

{

$bookView = $this->model->getBook($id);

Page 5: Tutoria mvc framework

Include “view/bookview.php”;

}

}

3. Xây dựng View:

//file view/booklist.php

<html>

<head><title>Book List</title></head>

<body>

<table>

<tr>

<td>Title</td>

<td>Decscription</td>

<td>Price</td>

</tr>

<?php

foreach ($listBooks as $book)

{

echo '<tr>

<td>

<a href="index.php?book='.$book[“title”].'">'

.$book[“title”].

'</a></td><td>'

.$book[“description”].'</td><td>'

.$book[“price”].'</td>

</tr>';

}

?>

</table>

</body>

</html>

Page 6: Tutoria mvc framework

//file view/bookview.php

<html>

<head><title>Book View</title></head>

<body>

<h3><?= $bookView[“title”]; ?></h3>

<span class=”description”> <?= $bookView[“description”];?></span>

<span class=”price”><?= number_format($bookView[“price”];?></span>

</body>

</html>

4. Trang index:

<?php

$c = $_GET[“controller”] || “home”;

$act = $_GET[“action”];

Include “controller/$c_controller.php”;

$c = $c.”_controller”;

$controller = new $c;

$controller->invoke();

?>

V. Mở rộng:

- Xây dựng thêm hàm loader để load các thư viện cần thiết.- Xây dựng thêm các folder tương ứng để thực hiện các chức năng mở rộng

+ folder cache + folder helper+ folder library+ folder plugin

Page 7: Tutoria mvc framework

- Cái này tùy các bạn có thể cần gì thì mình bổ sung, tham khảo thêm ở 1 số mvc framework hiện nay , codeigniter chẳng hạn.

Chúc các bạn học vui.