Professional Documents
Culture Documents
2022
1 Mô hình MVC
2 Khung phát triển ứng dụng web
CÔNG NGHỆ WEB AN TOÀN
3 Giới thiệu Laravel
Bài 5.2 Khung phát triển ứng dụng web
4 Ứng dụng CRUD với Laravel
1 Mô hình MVC
1. Hiểu được bản chất, đặc điểm của mẫu phát triển ứng dụng 2 Khung phát triển ứng dụng web
web MVC
2. Có khả năng đánh giá, lựa chọn, sử dụng khung phát triển 3 Giới thiệu Laravel
ứng dụng web
Model-View-Controller Model
• MVC là một mẫu thiết kế trong phát triển ứng dụng web • Model trực tiếp thực hiện các tác vụ trên dữ liệu, thường là
• MVC giúp tách biệt phần thể hiện thông tin với cơ chế xử lý thao tác với database.
thông tin. • Nói cách khác, Model chịu trách nhiệm bảo trì dữ liệu.
Controller • Nhìn chung, Model có tính tự trị cao,
nó không cần biết về sự tồn tại Controller
của Controller và View
Model View
Model View
1
13.12.2022
View Controller
• View chịu trách nhiệm hiển thị thông tin cho người dùng; tức • Controller là thành phần chịu trách nhiệm tương tác với
là sinh ra mã (HTML) của trang web. người dùng.
• View cần sử dụng dữ liệu do Model quản lý. • Controller có vai trò chính trong logic làm việc của ứng dụng
web, điều phối hoạt động của Model và View.
Controller Controller
Slide 10 of 102
2
13.12.2022
Ưu điểm
• Mô-đun hóa nên mã trong sáng: một ứng dụng có nhiều
C, nhiều M, nhiều V; mỗi mô-đun thường khá ngắn. • Hierarchical MVC
• Chuyên môn hóa công việc • Model-View-ViewModel
• Cộng tác, phát triển song song Controller • Model-View-Presenter
Nhược điểm • (còn nữa)
• Khó debug
Model View
• Khó unit test
Slide 13 of 102 Slide 14 of 102
Web Framework
Khung phát triển ứng dụng web (web framework, web
1 Mô hình MVC application framework) là khung phát triển phần mềm dành
cho việc phát triển web, bao gồm web application, web
2 Khung phát triển ứng dụng web services, web resources, and web APIs
3
13.12.2022
Web frameworks technologies market share Open source Backend Frameworks by "GitHub star"
Slide 24 of 102
4
13.12.2022
Slide 26 of 102
Slide 30 of 102
5
13.12.2022
Slide 33 of 102
\routes\web.php \resources\views\welcome.blade.php
6
13.12.2022
Slide 37 of 102
Debug Laravel
• Debugger là công cụ hữu ích, cần có khi lập trình
• Debug ứng dụng web phức tạp hơn so với debug ứng dụng
thông thường
• Công cụ How to Use Laravel Debugbar
̶ PHP: var_dump(), print_r() https://www.youtube.com/watch?v=DCoYynZ45Ws
̶ Laravel: dd()
̶ Chuyên dụng: xdebug, debugbar
Slide 39 of 102
1 Mô hình MVC
Cài đặt Xdebug cho VSCode 2 Khung phát triển ứng dụng web
trên Windows
https://tanakma.blogspot.com/2022/09/php-xdebug-vscode.html 3 Giới thiệu Laravel
4 Ứng dụng CRUD với Laravel
7
13.12.2022
Slide 43 of 102
8
13.12.2022
Sườn của migration 'contacts' Chỉnh lại cấu trúc bảng 'contacts'
(trong tập tin migration)
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('first_name');
$table->string('last_name');
$table->string('email');
});
}
9
13.12.2022
10
13.12.2022
Update:
Method Route Controller method Method Route
1. Lấy form để chỉnh sửa một contact
Controller method
GET /contacts index() GET 2. Nhận thông
/contacts
tin mới về một contact và index()
GET /contacts/{contact} show() GET cập nhật vào CSDL
/contacts/{contact} show()
GET /contacts/create create() GET /contacts/create create()
POST /contacts store() POST /contacts store()
GET /contacts/{contact}/edit edit() GET /contacts/{contact}/edit edit()
PUT/PATCH /contacts/{contact} update() PUT/PATCH /contacts/{contact} update()
Create:
DELETE /contacts/{contact}
1. Lấy form để nhập thông tin một contact destroy() DELETE /contacts/{contact} destroy()
2. Nhận một contact (mới) và ghi vào CSDL
Slide 61 of 102 Slide 62 of 102
11
13.12.2022
base.blade.php base.blade.php
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Demo Laravel CRUD app</title> <title>Demo Laravel CRUD app</title>
@vite(['resources/sass/app.scss']) @vite(['resources/sass/app.scss'])
</head> </head>
<body> <body>
• Vite là công cụ phát triển front-end
<div class="container"> @yield('main') </div> • <div
Chỉ class="container">
thị @vite sinh ra các@yield('main')
thẻ HTML để sử dụng
</div>
@vite(['resources/js/app.js']) các tài sản (asset) như CSS, JavaScript, images...
@vite(['resources/js/app.js'])
</body> • Kết quả ở đây là:
</body>
</html> <link rel="stylesheet"
</html>
href="http://localhost/testvite/public/build/assets/xxxx.css" />
Slide 69 of 102 Slide 70 of 102
base.blade.php base.blade.php
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
Kết quả ở đây là:
<head>
<script type="module" <head> Nội dung này sẽ được thay thế bởi
<meta name="viewport" content="width=device-width,initial-scale=1.0">
src="http://localhost/testvite/public/build/assets/yyyy.js"> <meta name="viewport" content="width=device-width,initial-scale=1.0">
@section('main') trong các template con
<title>Demo
</script> Laravel CRUD app</title> <title>Demo Laravel CRUD app</title>
@vite(['resources/sass/app.scss']) @vite(['resources/sass/app.scss'])
</head> </head>
<body> <body>
<div class="container"> @yield('main') </div> <div class="container"> @yield('main') </div>
@vite(['resources/js/app.js']) @vite(['resources/js/app.js'])
</body> </body>
</html> </html>
12
13.12.2022
create.blade create.blade
@extends('layouts.base') @extends('layouts.base')
@section('main') @section('main')
<div class="row"> <div class="col-sm-8 offset-sm-2"> <div class="row"> <div class="col-sm-8 offset-sm-2">
<form method="post" action="{{ route('contacts.store') }}"> <form method="post" action="{{ route('contacts.store') }}">
@csrf @csrf 'create' kế thừa 'base'
<div class="form-group"> <div class="form-group">
<label for="first_name">Họ, đệm:</label> <label for="first_name">Họ, đệm:</label>
<input type="text" class="form-control" name="first_name"/> </div> <input type="text" class="form-control" name="first_name"/> </div>
<div class="form-group"> <div class="form-group">
<label for="last_name">Tên:</label> <label for="last_name">Tên:</label>
<input type="text" class="form-control" name="last_name"/> </div> <input type="text" class="form-control" name="last_name"/> </div>
<div class="form-group"> <div class="form-group">
<label for="email">Email:</label> <label for="email">Email:</label>
<input type="text" class="form-control" name="email"/> </div> <input type="text" class="form-control" name="email"/> </div>
<button type="submit" class="btn btn-primary">Add contact</button> <button type="submit" class="btn btn-primary">Add contact</button>
</form> </form>
</div> </div> </div> </div>
@endsection @endsection
create.blade create.blade
@extends('layouts.base') @extends('layouts.base')
@section('main') @section('main')
<div class="row"> <div class="col-sm-8 offset-sm-2"> <div class="row"> <div class="col-sm-8 offset-sm-2">
<form method="post" action="{{ route('contacts.store') }}"> <form method="post" action="{{ route('contacts.store') }}">
@csrf @csrf
<div class="form-group"> <div class="form-group">
• create định nghĩa lại vùng main trong base
<label for="first_name">Họ, đệm:</label> <label for="first_name">Họ, đệm:</label>
<input type="text" class="form-control" name="first_name"/> </div> Trả về URL có dạng
<input type="text" class="form-control" name="first_name"/> </div>
<div class="form-group"> • tức là @yield('main') trong base sẽ được thay <div class="form-group">
<label for="last_name">Tên:</label> http(s)://domain/public/contacts
<label for="last_name">Tên:</label>
thế bởi nội dung của @section('main') trong
<input type="text" class="form-control" name="last_name"/> </div> <input type="text" class="form-control" name="last_name"/> </div>
<div class="form-group"> template con (ở đây là create) <div class="form-group">
<label for="email">Email:</label> <label for="email">Email:</label>
<input type="text" class="form-control" name="email"/> </div> <input type="text" class="form-control" name="email"/> </div>
<button type="submit" class="btn btn-primary">Add contact</button> <button type="submit" class="btn btn-primary">Add contact</button>
</form> </form>
</div> </div> </div> </div>
@endsection @endsection
13
13.12.2022
Kết quả Create: redirect Kết quả Create: cập nhật CSDL
14
13.12.2022
15
13.12.2022
Lập trình tác vụ 'Update': edit Lập trình tác vụ 'Update': redirect
16
13.12.2022
1 Mô hình MVC
2 Khung phát triển ứng dụng web
3 Giới thiệu Laravel
4 Ứng dụng CRUD với Laravel
Slide 99 of 102
1. Ở view 'index', hãy thêm nút "Thêm contact" vào cuối danh
sách (để gọi tác vụ 'create')
2. Ở view 'index', hãy bổ sung mã để hiển thị hộp thoại xác
nhận (confirmation) mỗi khi người dùng chọn tác vụ Delete
3. Tìm hiểu việc bổ sung Authentication Scaffolding vào một
ứng dụng Laravel
4. Tim hiểu việc triển khai ứng dụng Laravel lên môi trường
vận hành
Slide 102 of 102
17