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
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
4. Ứng dụng MVC (2)
− 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ì.
3
3
3
1. Model View Controller (MVC)
1. Mơ hình MVC
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
4. Ứng dụng MVC (2)
− 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.
− 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
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
4. Ứng dụng MVC (2)
❖ Ưu điểm:
− Thể hiện tính chun 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.
− u cầu về chun 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
2. Ưu/nhược điểm
− Controller: nơi đầu tiên nhận các yêu cầu (request).
− Controller được gọi từ file index.php.
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)
index.php
include_once("controller/Controller.php");
$controller = new Controller();
$controller->invoke();
7
7
7
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)
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';
}
else{
$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
để lấy dữ liệu => gửi dữ liệu ra view.
8
8
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
1. Mơ hình MVC
2. Ưu/nhược điểm
include_once("model/Student.php");
class StudentModel {
public function getStudentList(){
return array(
StudentModel.php
"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
public function getStudent($id){
4. Ứng dụng MVC (2)
$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
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)
Student.php
class Student {
private $id;
private $name;
private $birthday;
private $gender;
private $address;
public function getID(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function getBirthday(){
return $this->birthday;
}
public function getGender(){
return $this->gender;
}
public function getAddress(){
return $this->address;
}
}
public function __construct($id, $name,
$birthday, $gender, $address){
$this->id = $id;
$this->name = $name;
$this->birthday = $birthday;
$this->gender = $gender;
$this->address = $address;
}
11
11
View
1. Mơ hình MVC
2. Ưu/nhược điểm
− 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,…).
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)
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>
12
12
12
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
4. Ứng dụng MVC (2)
<table id="customers">
<tr><th>Mã số</th><th>Họ và tên</th><th>Ngày sinh</th><th>Địa chỉ</th></tr>
foreach ($students as $list => $student){
echo ‘
<tr><td><a href="index.php?id='.$student->getID().'">'.$student->getID().'</a></td>
<td>'.$student->getName().'</td>
<td>'.$student->getBirthday().'</td>
<td>'.$student->getAddress().'</td>
</tr>';
}
?>
</table>
13
13
13
Demo
http://localhost/MVC/mvc-student/index.php
1. Mơ hình MVC
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
View: student-list.php
3.1 Trang chủ
3.2 Controller
3.3 Model
3.4 View
4. Ứng dụng MVC (2)
View: student.php
14
14
14
Ứ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
include_once("controller/Controller.php");
include_once("controller/Controller.php");
$controller = new Controller();
$controller->invoke();
$controller = new Controller();
$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 {
private $modelStudent;
public function __construct(){
$this->modelStudent = new
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';
}
else{
$student = $this->modelStudent>getStudent($_GET['id']);
include 'view/student.php';
}
}
}
StudentModel((new Database())->getConnection());
}
public function invoke(){
if (!isset($_GET['id'])){
$students = $this->modelStudent>getStudentList();
include 'view/student-list.php';
}
else{
$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 {
public function getStudentList(){
return array(
include_once("model/Student.php");
class StudentModel {
private $connection;
public function __construct($db) {
$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) {
$row = mysqli_fetch_assoc ( $result );
$student = new Student($row["id"],
$row["name"],$row["birthday"],$row["gender"],$row["address"]);
return $student;
public function getStudent($id){
$allBooks = $this->getStudentList();
return $allBooks[$id];
}
}
}
return null;
}
}
19
❑ Ứng dụng 1:
❑ Ứng dụng 2:
Student.php
Student.php
class Student {
private $id;
private $name;
private $birthday;
private $gender;
private $address;
public function getID(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function getBirthday(){
return $this->birthday;
}
public function getGender(){
return $this->gender;
}
public function getAddress(){
return $this->address;
}
public function __construct($id, $name, $birthday, $gender, $address){
$this->id = $id;
$this->name = $name;
$this->birthday = $birthday;
$this->gender = $gender;
$this->address = $address;
class Student {
private $id;
private $name;
private $birthday;
private $gender;
private $address;
public function getID(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function getBirthday(){
return $this->birthday;
}
public function getGender(){
return $this->gender;
}
public function getAddress(){
return $this->address;
}
public function __construct($id, $name, $birthday, $gender, $address){
$this->id = $id;
$this->name = $name;
$this->birthday = $birthday;
$this->gender = $gender;
$this->address = $address;
}
}
}
}
20
20
❑ Ứng dụng 1:
❑ Ứng dụng 2:
view/student.php
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>
<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 date('d/m/Y', strtotime($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>
21
21
21
❑ Ứng dụng 1:
❑ Ứng dụng 2:
view/student-list.php
view/student-list.php
<table id="customers">
<table id="customers">
<tr>
<th>Mã số</th>
<th>Họ và tên</th>
<th>Ngày sinh</th>
<th>Địa chỉ</th>
</tr>
foreach ($students as $student){
echo
'<tr>
<td><a href="index.php?id='.$student->getID().'">'.$student>getID().'</a></td>
<td>'.$student->getName().'</td>
<td>'.date('d-m-Y', strtotime($student->getBirthday())).'</td>
<td>'.$student->getAddress().'</td>
</tr>';
}
?>
</table>
<tr>
<th>Mã số</th>
<th>Họ và tên</th>
<th>Ngày sinh</th>
<th>Địa chỉ</th>
</tr>
foreach ($students as $list => $student){
echo
‘<tr>
<td><a href="index.php?id='.$student->getID().'">'.$student>getID().'</a></td>
<td>'.$student->getName().'</td>
<td>'.$student->getBirthday().'</td>
<td>'.$student->getAddress().'</td>
</tr>';
}
?>
</table>
22
22
22
Demo
http://localhost/MVC/mvc-student-datebase/index.php
View: student-list.php
View: student.php
23
23
Tài liệu tham khảo
1. Mơ hình MVC
1.
5/6/2018.
2. Ưu/nhược điểm
2.
5/6/2018
3. Ứng dụng MVC (1)
3.
/>
4. Ứng dụng MVC (2)
4.
6/6/2018.
Tài liệu tham khảo
5.
6/6/2018.
24
24
24