1. PHP là gì?
PHP là ngôn ngữ lập trình phía server, thường dùng để xây dựng backend và web application.
Flow cơ bản:
Client
↓ HTTP Request
PHP / Laravel
↓
Business Logic
↓
Database
↓
HTTP Response
Laravel được xây dựng trên PHP, nên muốn học Laravel cần nắm những nền tảng PHP quan trọng trước.
2. PHP Fundamentals
Các phần PHP cần nắm:
Variable & Data Type
$name = "John";
$age = 20;
$isActive = true;
Các kiểu thường gặp:
string
int
float
bool
array
object
null
Array
PHP sử dụng array rất nhiều:
$user = [
'name' => 'John',
'age' => 20
];
Condition & Loop
if ($age >= 18) {
// ...
}
foreach ($users as $user) {
// ...
}
Function
function add(int $a, int $b): int
{
return $a + $b;
}
3. PHP OOP
Đây là phần rất quan trọng vì Laravel sử dụng Object-Oriented Programming gần như xuyên suốt framework.
Cần hiểu:
- Class.
- Object.
- Property.
- Method.
- Constructor.
- Encapsulation.
- Inheritance.
- Interface.
- Trait.
- Namespace.
Class và Object
Class là bản thiết kế, Object là instance được tạo từ class.
class User
{
public string $name;
public function getName(): string
{
return $this->name;
}
}
Tạo object:
$user = new User();
Trong Laravel, rất nhiều thứ là class/object:
Controller
Model
Request
Service
Middleware
Resource
4. Constructor và Dependency Injection
Constructor chạy khi object được tạo.
class UserService
{
public function __construct(
private UserRepository $repository
) {
}
}
Ở đây UserService cần UserRepository.
Thay vì tự tạo:
$this->repository = new UserRepository();
ta truyền dependency vào:
UserService(UserRepository)
Đây gọi là Dependency Injection.
Laravel có Service Container giúp tự động tạo và inject những dependency cần thiết.
Đây là một trong những concept quan trọng nhất của Laravel.
5. Laravel là gì?
Laravel là PHP framework dùng để xây dựng web application và REST API.
Laravel cung cấp sẵn nhiều thành phần:
Routing
Controller
Middleware
Validation
Authentication
Authorization
Eloquent ORM
Database
Queue
Cache
Testing
Mục tiêu là giúp developer xây backend có cấu trúc thay vì tự xây mọi thứ từ PHP thuần.
Route
Route xác định request sẽ đi đến đâu.
Route::get('/users', [UserController::class, 'index']);
Ví dụ:
GET /users
→ Laravel gọi UserController@index.
Các HTTP method cần nắm:
GET
POST
PUT
PATCH
DELETE
Controller
Controller tiếp nhận request và điều phối xử lý.
class UserController
{
public function index()
{
return User::all();
}
}
Controller không nên chứa toàn bộ business logic.
Khi logic phức tạp, có thể đưa sang Service.
Request & Validation
Request chứa dữ liệu client gửi lên:
Query
Path Parameter
Body
Header
File
Laravel hỗ trợ validation:
$request->validate([
'name' => ['required', 'string'],
'email' => ['required', 'email'],
]);
Điểm quan trọng Không được tin tưởng dữ liệu từ client. Server phải validate dữ liệu trước khi xử lý.
6. Eloquent Relationship
Trong application thực tế, các Model thường liên quan với nhau.
Ví dụ:
User
↓
hasMany
↓
Post
Một user có nhiều post.
Trong Laravel:
public function posts()
{
return $this->hasMany(Post::class);
}
Các relationship quan trọng:
One-to-One
User → Profile
Một user có một profile.
One-to-Many
User → Posts
Một user có nhiều post.
Many-to-Many
User ↔ Role
Một user có nhiều role và một role có nhiều user.
Relationship giúp Eloquent hiểu cách các entity liên kết với nhau trong database.
7. Dependency Injection và Service Container
Đây là phần liên quan trực tiếp đến Service.
Ví dụ:
class UserService
{
public function __construct(
private UserRepository $repository
) {
}
}
UserService cần UserRepository.
Laravel Service Container có thể tạo UserRepository rồi inject vào UserService.
Ta không cần tự quản lý việc tạo dependency ở mọi nơi.
Lợi ích:
- Giảm coupling.
- Dễ thay implementation.
- Dễ testing.
- Code dễ quản lý hơn.
8. Artisan
Artisan là command-line tool của Laravel.
Ví dụ:
php artisan serve
Chạy application local.
php artisan route:list
Xem danh sách route.
php artisan make:controller UserController
Tạo Controller.
php artisan make:model User -m
Tạo Model và Migration.
php artisan migrate
Chạy migration.
Artisan giúp developer thao tác với Laravel project nhanh và thống nhất hơn.
Chốt lại
PHP cung cấp ngôn ngữ và nền tảng OOP.
Laravel cung cấp framework để tổ chức application:
Route → Middleware → Controller → Validation → Service → Model/Eloquent → Database → Response
Trong đó:
- Route: request đi đâu.
- Middleware: request có được đi tiếp không.
- Controller: điều phối request.
- Validation: dữ liệu có hợp lệ không.
- Service: xử lý business logic.
- Model/Eloquent: làm việc với database.
- Database: lưu dữ liệu.
- Response: trả kết quả cho client.
- Authentication: user là ai.
- Authorization: user được làm gì.







Khoá học lập trình game con rắn cho trẻ em




