Chuyển đến nội dung chính

Bài 1: Giới thiệu Laravel — Framework PHP Elegant nhất

Laravel là gì, triết lý "elegant syntax". So sánh Laravel vs Symfony vs Django vs Spring. PHP 8.4 essentials (readonly, enums, fibers, named arguments). Laravel Sail, Herd setup.

💻 Lập trình — Bài 1 Bài 1: Giới thiệu Laravel — Framework PHP Elegant nhất

Laravel: Từ Cơ bản đến Nâng cao

Phần 1: Laravel Fundamentals

xdev.asia

1. Laravel là gì?

Laravel là PHP framework phổ biến nhất, theo triết lý "elegant syntax" — code sạch, dễ đọc, developer experience đặt lên hàng đầu. Laravel cung cấp hệ sinh thái hoàn chỉnh: ORM (Eloquent), queue, cache, auth, testing, deployment.

FrameworkNgôn ngữORMƯu điểm
LaravelPHPEloquentHệ sinh thái lớn, DX tuyệt vời
SymfonyPHPDoctrineEnterprise, linh hoạt
DjangoPythonDjango ORMBatteries-included, admin built-in
Spring BootJavaJPA/HibernateEnterprise, microservices

2. PHP 8.4 Essentials

<?php
// Enums
enum UserRole: string {
    case Admin = 'admin';
    case Staff = 'staff';
    case Customer = 'customer';
}

// Readonly properties
class Product {
    public function __construct(
        public readonly string $name,
        public readonly float $price,
    ) {}
}

// Named arguments
$user = User::create(
    name: 'John',
    email: '[email protected]',
    role: UserRole::Customer,
);

// Match expression
$label = match($status) {
    'active' => 'Hoạt động',
    'inactive' => 'Tạm dừng',
    default => 'Không xác định',
};

3. Cài đặt Laravel

# Với Composer
composer create-project laravel/laravel my-app

# Với Laravel Installer
composer global require laravel/installer
laravel new my-app

# Với Laravel Sail (Docker)
curl -s "https://laravel.build/my-app?with=pgsql,redis" | bash
cd my-app && ./vendor/bin/sail up

4. Cấu trúc thư mục

my-app/
├── app/
│   ├── Http/Controllers/
│   ├── Models/
│   └── Providers/
├── config/
├── database/
│   ├── factories/
│   ├── migrations/
│   └── seeders/
├── resources/views/
├── routes/
│   ├── web.php
│   └── api.php
├── storage/
├── tests/
├── .env
└── artisan
# Artisan commands
php artisan serve          # Dev server
php artisan make:model Product -mfc  # Model + migration + factory + controller
php artisan migrate        # Chạy migrations
php artisan tinker         # REPL

Bài tiếp theo: Routing, Controllers & Middleware.