Bài giảng Phát triển phần mềm nguồn mở - Bài 5: Mô hình MVC trong PHP - Nguyễn Hữu Thể

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

pdf 24 trang yennguyen 3640
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phát triển phần mềm nguồn mở - Bài 5: 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

Tóm tắt nội dung tài liệu: Bài giảng Phát triển phần mềm nguồn mở - Bài 5: Mô hình MVC trong PHP - Nguyễn Hữu Thể

Bài giảng Phát triển phần mềm nguồn mở - Bài 5: Mô hình MVC trong PHP - Nguyễn Hữu Thể
MÔ HÌNH MVC TRONG PHP
12/31/2018 1
NGUYỄN HỮU THỂ
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
12/31/2018
2
31. Model View Controller (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ì.
3
3
1. Mô hình MVC
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
4. Ứng dụng MVC (2)
41. Model View Controller (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.
− 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
1. Mô hình MVC
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
4. Ứng dụng MVC (2)
52. Ưu và nhược điểm của MVC
❖ Ưu điểm:
− 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
1. Mô hình MVC
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
4. Ứng dụng MVC (2)
63. Thiết kế ứng dụng Web theo mô hình MVC
− Cấu trúc ứng dụng MVC:
6
6
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)
7index.php
− Controller: nơi đầu tiên nhận các yêu cầu (request).
− Controller được gọi từ file index.php. 
index.php
7
<?php
include_once("controller/Controller.php");
$controller = new Controller();
$controller->invoke();
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)
8 8
8
<?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/c';
}
}
}
Controller.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.
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)
9MVC Sequence Diagram
9
9
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)
10
10
− Model đại diện cho dữ liệu và logic của ứng dụng, thường hay gọi là 
business logic.
include_once("model/Student.php");
class StudentModel {
public function getStudentList(){
return array(
"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ơ")
);
}
public function getStudent($id){
$allBooks = $this->getStudentList();
return $allBooks[$id];
}
}
StudentModel.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)
11
11
Student.phpclass 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;
}
}
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)
12
View
− 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,).
12
12
Mã sốgetID()?>
Họ và têngetName()?>
Ngày sinhgetBirthday()?>
Giới tínhgetGender()?>
Địa chỉgetAddress()?>
view/student.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)
13 13
13
view/student-list.php
Mã sốHọ và tênNgày sinhĐịa chỉ
<?php
foreach ($students as $list => $student){
echo ‘
getID().'">'.$student->getID().'
'.$student->getName().'
'.$student->getBirthday().'
'.$student->getAddress().'
';
}
?>
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)
14
Demo
14
14
View: student-list.php
View: student.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)
15
ỨNG DỤNG MVC CÓ DATABASE
❑ Cấu trúc ứng dụng 1:
15
15
❑ Cấu trúc ứng dụng 2: 
bổ sung database
✓ File Database.php có thể 
lưu ở 1 thưc mục khác, 
VD: library
1. Mô hình MVC
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
4. Ứng dụng MVC (2)
4.1 Trang chủ 
4.2 Controller
4.3 Model
4.4 View
16
Bổ sung lớp Database.php
16
16
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 );
}
}
}
Chứa phương thức kết nối/ hủy kết nối đến database
17 17
<?php
include_once("controller/Controller.php");
$controller = new Controller();
$controller->invoke();
17
❑ Ứng dụng 1: ❑ Ứng dụng 2:
<?php
include_once("controller/Controller.php");
$controller = new Controller();
$controller->invoke();
index.php index.php
18 18
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';
}
}
}
Controller.php
❑ Ứng dụng 1: ❑ Ứng dụng 2:
include_once("model/Database.php");
include_once("model/StudentModel.php");
class Controller {
private $modelStudent;
public function __construct(){
$this->modelStudent = new 
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';
}
}
}
Controller.php
19
include_once("model/Student.php");
class StudentModel {
public function getStudentList(){
return array(
"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ơ")
);
}
public function getStudent($id){
$allBooks = $this->getStudentList();
return $allBooks[$id];
}
}
StudentModel.php
❑ Ứng dụng 1: ❑ Ứng dụng 2:
StudentModel.php
include_once("model/Student.php");
class StudentModel {
private $connection;
public function __construct($db) {
$this->connection = $db;
}
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;
}
return null;
}
}
20
20
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;
}
}
Student.php
❑ Ứng dụng 1: ❑ Ứng dụng 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;
}
}
21 21
21
Mã sốgetID()?>
Họ và têngetName()?>
Ngày sinhgetBirthday()?>
Giới tínhgetGender()?>
Địa chỉgetAddress()?>
view/student.php
Mã sốgetID()?>
Họ và têngetName()?>
Ngày sinh
getBirthday()))?>
Giới tínhgetGender()?>
Địa chỉgetAddress()?>
❑ Ứng dụng 1: ❑ Ứng dụng 2:
view/student.php
22 22
22
view/student-list.php
Mã số
Họ và tên
Ngày sinh
Địa chỉ
<?php
foreach ($students as $list => $student){
echo 
‘
getID().'">'.$student-
>getID().'
'.$student->getName().'
'.$student->getBirthday().'
'.$student->getAddress().'
';
}
?>
❑ Ứng dụng 1: ❑ Ứng dụng 2:
view/student-list.php
Mã số
Họ và tên
Ngày sinh
Địa chỉ
<?php
foreach ($students as $student){
echo 
'
getID().'">'.$student-
>getID().'
'.$student->getName().'
'.date('d-m-Y', strtotime($student->getBirthday())).'
'.$student->getAddress().'
';
}
?>
23
Demo
23
View: student-list.php
View: student.php
24
Tài liệu tham khảo
1.  5/6/2018.
2. https://whatis.techtarget.com/definition/model-view-controller-MVC, 5/6/2018
3. https://www.tutorialspoint.com/mvc_framework/mvc_framework_introduction.htm
4. https://laravel.com/docs, 6/6/2018.
5. https://www.w3schools.com/css/css_table.asp, 6/6/2018.
24
24
1. Mô hình MVC
2. Ưu/nhược điểm
3. Ứng dụng MVC (1)
4. Ứng dụng MVC (2)
Tài liệu tham khảo

File đính kèm:

  • pdfbai_giang_phat_trien_phan_mem_nguon_mo_chuong_5_mo_hinh_mvc.pdf