Bài giảng Phát triển ứng dụng Web - Bài 6: Mô hình MVC trong PHP - Nguyễn Hữu Thể

MÔ HÌNH MVC TRONG PHP  
NGUYỄN HỮU THỂ  
03/01/2021  
1
Nội dung  
❑Mô hình MVC  
❑Ưu và nhược điểm của mô hình MVC  
❑Phát triển ứng dụng Web theo mô hình MVC  
❑Tài liệu tham khảo  
2
03/01/2021  
1. Model View Controller (MVC)  
1. Mô hình MVC  
− MVC: một kiến trúc phần mềm (hay mô hình thiết kế) được sử  
dụng trong kỹ thuật phần mềm.  
− Tách một ứng dụng web ra làm 3 thành phần đảm nhiệm chức  
năng tách biệt, thuận tiện cho việc xử lý và bảo trì.  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
4. Ứng dụng MVC (2)  
3
3
3
1. Model View Controller (MVC)  
1. Mô hình MVC  
− Model: Quản lý dữ liệu, lưu trữ và truy xuất các dữ liệu từ cơ sở  
dữ liệu, các logic xử lý.  
− View: Hiển thị dữ liệu đã được truy xuất từ model.  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
4. Ứng dụng MVC (2)  
− Controller: Trung gian, giúp cho model và view tương tác với  
nhau.  
• Controller nhận request từ client.  
• Gọi các model để thực hiện các yêu cầu và gửi ra View.  
• View sẽ format lại data từ controller gửi ra và trình bày dữ liệu  
(HTML).  
4
4
4
2. Ưu và nhược điểm của MVC  
1. Mô hình MVC  
❖ Ưu điểm:  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
4. Ứng dụng MVC (2)  
− Thể hiện tính chuyên nghiệp trong lập trình, phân tích  
thiết kế.  
− Phát triển ứng dụng theo cấu trúc đơn giản, dễ nâng  
cấp, bảo trì, triển khai.  
=> Sử dụng phổ biến nhất trong các PHP Framework  
❖ Nhược điểm:  
− Tốn nhiều thời gian để xây dựng thư viện, cấu trúc.  
− Yêu cầu về chuyên môn khá cao, có kiến thức vững về  
các mô hình chuẩn.  
5
5
5
3. Thiết kế ứng dụng Web theo mô hình MVC  
1. Mô hình MVC  
− Cấu trúc ứng dụng MVC:  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
3.4 View  
4. Ứng dụng MVC (2)  
6
6
6
index.php  
1. Mô hình MVC  
− Controller: nơi đầu tiên nhận các yêu cầu (request).  
− Controller được gọi từ file index.php.  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
index.php  
<?php  
3.4 View  
include_once("controller/Controller.php");  
4. Ứng dụng MVC (2)  
$controller = new Controller();  
$controller->invoke();  
7
7
7
<?php  
Controller.php  
include_once("model/StudentModel.php");  
class Controller {  
private $modelStudent;  
public function __construct(){  
$this->modelStudent = new StudentModel();  
}
public function invoke(){  
if (!isset($_GET['id'])){  
$students = $this->modelStudent->getStudentList();  
include 'view/student-list.php';  
}
1. Mô hình MVC  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
3.4 View  
else{  
4. Ứng dụng MVC (2)  
$student = $this->modelStudent->getStudent($_GET['id']);  
include 'view/student.php';  
}
}
}
▪ Contructor: gọi và khởi tạo lớp Model.  
▪ Invoke: quyết định data nào được phép trả ra từ model => gọi model  
8
để lấy dữ liệu => gửi dữ liệu ra view.  
8
MVC Sequence Diagram  
1. Mô hình MVC  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
3.4 View  
4. Ứng dụng MVC (2)  
9
9
9
include_once("model/Student.php");  
class StudentModel {  
StudentModel.php  
public function getStudentList(){  
return array(  
1. Mô hình MVC  
2. Ưu/nhược điểm  
"01" => new Student("01", "Nguyễn Đình A", "15-06-2000","Nam", "Vĩnh Long"),  
"02" => new Student("02", "Nguyễn Đình B", "16-06-2000","Nam", "Vĩnh Long"),  
"03" => new Student("03", "Nguyễn Văn C", "17-06-2000","Nam", "Cần Thơ"),  
"04" => new Student("04", "Nguyễn Văn D", "18-06-2000","Nam", "Cần Thơ")  
);  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
}
3.4 View  
4. Ứng dụng MVC (2)  
public function getStudent($id){  
$allBooks = $this->getStudentList();  
return $allBooks[$id];  
}
}
− Model đại diện cho dữ liệu và logic của ứng dụng, thường hay gọi là  
business logic.  
10  
10  
class Student {  
private $id;  
Student.php  
private $name;  
private $birthday;  
private $gender;  
private $address;  
1. Mô hình MVC  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
public function getID(){  
return $this->id;  
}
public function getName(){  
return $this->name;  
3.4 View  
}
public function __construct($id, $name,  
$birthday, $gender, $address){  
$this->id = $id;  
$this->name = $name;  
$this->birthday = $birthday;  
$this->gender = $gender;  
$this->address = $address;  
}
4. Ứng dụng MVC (2)  
public function getBirthday(){  
return $this->birthday;  
}
public function getGender(){  
return $this->gender;  
}
public function getAddress(){  
return $this->address;  
}
}
11 11  
View  
1. Mô hình MVC  
− View: định đạng lại dữ liệu nhận được từ model.  
− Trình bày nhiều dạng dữ liệu (xml, json, array,…).  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
view/student.php  
<table>  
<tr><td>Mã số</td><td><?php echo $student->getID()?></td></tr>  
<tr><td>Họ và tên</td><td><?php echo $student->getName()?></td></tr>  
<tr><td>Ngày sinh</td><td><?php echo $student->getBirthday()?></td></tr>  
<tr><td>Giới tính</td><td><?php echo $student->getGender()?></td></tr>  
<tr><td>Địa chỉ</td><td><?php echo $student->getAddress()?></td></tr>  
</table>  
3.4 View  
4. Ứng dụng MVC (2)  
12  
12  
view/student-list.php  
<table id="customers">  
<tr><th>Mã số</th><th>Họ và tên</th><th>Ngày sinh</th><th>Địa chỉ</th></tr>  
1. Mô hình MVC  
2. Ưu/nhược điểm  
<?php  
foreach ($students as $list => $student){  
echo ‘  
<tr><td><a href="index.php?id='.$student->getID().'">'.$student->getID().'</a></td>  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
<td>'.$student->getName().'</td>  
<td>'.$student->getBirthday().'</td>  
<td>'.$student->getAddress().'</td>  
3.4 View  
4. Ứng dụng MVC (2)  
</tr>';  
}
?>  
</table>  
13  
13  
Demo  
1. Mô hình MVC  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
3.1 Trang chủ  
3.2 Controller  
3.3 Model  
View: student-list.php  
3.4 View  
4. Ứng dụng MVC (2)  
View: student.php  
ỨNG DỤNG MVC CÓ DATABASE  
1. Mô hình MVC  
2. Ưu/nhược điểm  
3. Ứng dụng MVC (1)  
❑ Cấu trúc ứng dụng 1:  
❑ Cấu trúc ứng dụng 2:  
bổ sung database  
4. Ứng dụng MVC (2)  
4.1 Trang chủ  
4.2 Controller  
4.3 Model  
4.4 View  
✓ File Database.php có thể  
lưu ở 1 thưc mục khác,  
VD: library  
15  
15  
15  
Bổ sung lớp Database.php  
Chứa phương thức kết nối/ hủy kết nối đến database  
class Database {  
private $connection;  
public function getConnection() {  
if (! ($this->connection)) {  
$this->connection = mysqli_connect ( 'localhost', 'root', '',  
'mvc_student' ) or die ( 'Không thể kết nối CSDL' );  
mysqli_set_charset ( $this->connection, 'utf8' );  
}
return $this->connection;  
}
public function closeConnection() {  
if ($this->connection) {  
mysqli_close ( $this->connection );  
}
}
}
16  
16  
16  
❑ Ứng dụng 1:  
❑ Ứng dụng 2:  
index.php  
index.php  
<?php  
<?php  
include_once("controller/Controller.php");  
include_once("controller/Controller.php");  
$controller = new Controller();  
$controller = new Controller();  
$controller->invoke();  
$controller->invoke();  
17  
17  
17  
❑ Ứng dụng 1:  
❑ Ứng dụng 2:  
Controller.php  
Controller.php  
include_once("model/StudentModel.php");  
include_once("model/Database.php");  
include_once("model/StudentModel.php");  
class Controller {  
class Controller {  
private $modelStudent;  
private $modelStudent;  
public function __construct(){  
public function __construct(){  
$this->modelStudent = new  
StudentModel();  
$this->modelStudent = new  
StudentModel((new Database())->getConnection());  
}
}
public function invoke(){  
public function invoke(){  
if (!isset($_GET['id'])){  
if (!isset($_GET['id'])){  
$students = $this->modelStudent-  
>getStudentList();  
include 'view/student-list.php';  
$students = $this->modelStudent-  
>getStudentList();  
include 'view/student-list.php';  
}
}
else{  
else{  
$student = $this->modelStudent-  
>getStudent($_GET['id']);  
include 'view/student.php';  
$student = $this->modelStudent-  
>getStudent($_GET['id']);  
include 'view/student.php';  
}
}
}
}
}
}
18  
18  
❑ Ứng dụng 1:  
❑ Ứng dụng 2:  
StudentModel.php  
StudentModel.php  
include_once("model/Student.php");  
class StudentModel {  
include_once("model/Student.php");  
class StudentModel {  
private $connection;  
public function __construct($db) {  
public function getStudentList(){  
return array(  
$this->connection = $db;  
"01" => new Student("01", "Nguyễn Đình  
}
A", "15-06-2000","Nam", "Vĩnh Long"),  
"02" => new Student("02", "Nguyễn Đình  
B", "16-06-2000","Nam", "Vĩnh Long"),  
"03" => new Student("03", "Nguyễn Văn C",  
"17-06-2000","Nam", "Cần Thơ"),  
"04" => new Student("04", "Nguyễn Văn  
D", "18-06-2000","Nam", "Cần Thơ")  
);  
function getStudentList() {  
$sql = "Select * from Student";  
$result = mysqli_query ( $this->connection, $sql );  
while ( $row = mysqli_fetch_array ( $result ) ) {  
$data [] = new Student($row["id"],  
$row["name"],$row["birthday"],$row["gender"],$row["address"]);  
}
return $data;  
}
}
function getStudent($id) {  
$sql = "Select * from Student where id = $id";  
$result = mysqli_query ( $this->connection, $sql );  
if (mysqli_num_rows ( $result ) > 0) {  
public function getStudent($id){  
$allBooks = $this->getStudentList();  
return $allBooks[$id];  
}
$row = mysqli_fetch_assoc ( $result );  
$student = new Student($row["id"],  
}
$row["name"],$row["birthday"],$row["gender"],$row["address"]);  
return $student;  
}
return null;  
}
}
19  
❑ Ứng dụng 1:  
❑ Ứng dụng 2:  
Student.php  
Student.php  
class Student {  
class Student {  
private $id;  
private $id;  
private $name;  
private $name;  
private $birthday;  
private $birthday;  
private $gender;  
private $gender;  
private $address;  
private $address;  
public function getID(){  
public function getID(){  
return $this->id;  
return $this->id;  
}
}
public function getName(){  
public function getName(){  
return $this->name;  
return $this->name;  
}
}
public function getBirthday(){  
public function getBirthday(){  
return $this->birthday;  
return $this->birthday;  
}
}
public function getGender(){  
public function getGender(){  
return $this->gender;  
return $this->gender;  
}
}
public function getAddress(){  
public function getAddress(){  
return $this->address;  
return $this->address;  
}
}
public function __construct($id, $name, $birthday, $gender, $address){  
public function __construct($id, $name, $birthday, $gender, $address){  
$this->id = $id;  
$this->name = $name;  
$this->birthday = $birthday;  
$this->gender = $gender;  
$this->address = $address;  
}
$this->id = $id;  
$this->name = $name;  
$this->birthday = $birthday;  
$this->gender = $gender;  
$this->address = $address;  
}
2020  
}
}
Tải về để xem bản đầy đủ
pdf 24 trang yennguyen 30401 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phát triển ứng dụng Web - Bài 6: Mô hình MVC trong PHP - Nguyễn Hữu Thể", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_phat_trien_ung_dung_web_bai_6_mo_hinh_mvc_trong_ph.pdf