Laravel Framework PHP Instructions From Scratch [Installation & Development]

Laravel Framework PHP Hướng Dẫn Từ Đầu [Cài Đặt & Phát Triển]

Is it difficult to jump into Laravel? Honestly, when I first started, I felt a bit "overwhelmed" by such a large framework. But believe me, once you have grasped the core concepts and successfully installed your first project yourself, you will find the road ahead is much more open. This article sharing on the topic Laravel framework PHP instructions from scratch is my real-life experience, helping you go from zero to running your first application in the fastest way.

Why Laravel and not pure PHP or another framework?

Laravel là một PHP framework mạnh mẽ cung cấp các công cụ và cấu trúc sẵn có giúp phát triển ứng dụng web nhanh chóng, an toàn và dễ bảo trì hơn so với việc code PHP thuần.

Quick introduction: What is Laravel and the actual benefits it brings

Laravel là một PHP framework mã nguồn mở miễn phí, tuân theo kiến trúc Model View Controller (MVC), ra đời nhằm mục đích giúp việc phát triển web trở nên thanh lịch và thú vị hơn.

To understand more deeply, if you are still wondering what php is, this is the first step to understand before going any further. At Pham Hai, we see that Laravel's benefits lie in its rich ecosystem and extremely strong Laravel community. It provides built-in features such as Authentication (user authentication), Routing (routing) and a proactive security system against vulnerabilities such as SQL Injection or CSRF.

In particular, Laravel features help reduce the time spent writing repetitive code. Thay vì tự xây dựng mọi thứ từ đầu, bạn có thể tận dụng cú pháp Laravel cực kỳ dễ đọc và thanh lịch. Điều này giúp các lập trình viên tập trung vào việc xử lý logic nghiệp vụ, mang lại hiệu suất làm việc cao hơn hẳn. Đây là lý do tại sao nhiều khóa học Laravel luôn thu hút đông đảo học viên và học Laravel cho người mới bắt đầu trở thành xu hướng tất yếu của thị trường tuyển dụng.

Gentle comparison: When should you use Laravel instead of pure PHP code?

Bạn nên dùng Laravel khi xây dựng các dự án vừa và lớn, yêu cầu tính bảo mật cao, dễ dàng mở rộng và làm việc nhóm, thay vì code PHP thuần dễ dẫn đến mã nguồn lộn xộn.

If you are building a website that displays simple information, plain PHP may be enough. For beginners, Learning basic PHP backend web programming is a solid foundation that cannot be ignored. However, as the project grew, Laravel's advantages and disadvantages began to clearly show their superiority.

When coding pure PHP, you have to design your own directory structure, write your own database connection functions, and manage information security yourself. Meanwhile, building web applications with Laravel helps you standardize everything according to international standards. It brings uniformity, helping team members easily read and understand each other's code. Furthermore, with the support of the very detailed Laravel documentation on the homepage, debugging becomes much easier.

"Kitchen" preparation - The environment needed before "coding"

Để cài đặt Laravel, bạn cần chuẩn bị môi trường bao gồm PHP phiên bản phù hợp, công cụ quản lý thư viện Composer và một máy chủ web ảo (Web server) như XAMPP hoặc Laragon.

Install Composer - The indispensable "conductor" of the PHP project

Composer là công cụ quản lý sự phụ thuộc (Dependency Management) cho PHP, giúp tự động tải về và cập nhật các thư viện mà dự án Laravel yêu cầu.

Bạn không thể cài đặt Laravel qua Composer nếu thiếu công cụ này. Hãy truy cập trang chủ getcomposer.org, tải file cài đặt (.exe cho Windows) và chạy nó. Trong quá trình cài đặt, Composer sẽ yêu cầu bạn chỉ định đường dẫn đến file php.exe nằm trong thư mục máy chủ ảo của bạn.

Đây là bước bắt buộc trong hướng dẫn cài đặt Laravel. Composer giống như một người quản gia, bạn chỉ cần khai báo "tôi cần thư viện này", nó sẽ tự động tìm, tải về và sắp xếp gọn gàng vào thư mục vendor. Để kiểm tra cài đặt thành công, hãy mở Terminal (hoặc Command Prompt) và gõ lệnh composer -v. Nếu thấy logo Composer hiện ra cùng phiên bản mới nhất, bạn đã hoàn thành xuất sắc bước này.

Install local environment (XAMPP, Laragon) - Which one is convenient to choose?

XAMPP và Laragon là hai phần mềm tạo môi trường máy chủ cục bộ phổ biến nhất, trong đó Laragon được đánh giá cao hơn về tốc độ và sự tiện dụng trên Windows.

Với những bạn đang tìm hiểu cài đặt Laravel trên Windows, mình khuyên dùng Laragon (phiên bản 8.6+). Nó cực kỳ nhẹ, khởi động nhanh và tự động tạo tên miền ảo (ví dụ: ten-du-an.test) rất tiện lợi. XAMPP cũng tốt, nhưng đôi khi việc cấu hình cổng (port) hay gặp rắc rối với các phần mềm khác như Skype hay VMWare.

Khi ứng dụng của bạn hoàn thành và sẵn sàng đưa lên mạng, việc Chọn hosting phù hợp cho website mới là bước tiếp theo rất quan trọng để đảm bảo tốc độ tải trang. Dù chọn công cụ nào ở môi trường local, cả XAMPP và Laragon đều cung cấp cho bạn một Web server (Apache hoặc Nginx) và hệ quản trị cơ sở dữ liệu (MySQL/MariaDB) cần thiết cho việc phát triển web.

Check PHP version - Make sure it's "age-matched" with the latest version of Laravel

Laravel 13 (ra mắt 03/2026) yêu cầu tối thiểu PHP 8.3, do đó bạn cần kiểm tra và cập nhật phiên bản PHP trên máy tính trước khi tiến hành cài đặt framework.

According to the latest updates as of March 2026, Laravel's latest version is Laravel 13, officially released by Taylor Otwell. This version requires PHP >= 8.3 and brings many groundbreaking features such as Native PHP Attributes. If you are maintaining legacy systems using Laravel 8, Laravel 9, Laravel 10, Laravel 11 or Laravel 12, the PHP requirements will be lower, but for new projects we always recommend using the latest version.

Để kiểm tra, hãy mở Terminal và gõ php -v. Nếu kết quả trả về nhỏ hơn 8.3, bạn cần nâng cấp XAMPP hoặc Laragon lên bản mới nhất. Đây là một trong những yêu cầu cài đặt Laravel mang tính quyết định. Ngoài ra, việc hiểu rõ cách các máy chủ xử lý PHP cũng rất hữu ích, bạn có thể tham khảo bài viết Nginx vs Apache so sánh web server 2026 để có cái nhìn tổng quan hơn về hiệu năng.

Hands-on: Install your first Laravel project in 5 minutes

Quá trình cài đặt Laravel project đầu tiên chỉ mất vài phút thông qua một dòng lệnh duy nhất của Composer, giúp tạo ra toàn bộ cấu trúc thư mục và tải về các gói cần thiết.

Chạy lệnh "thần thánh": composer create-project laravel/laravel ten-du-an

Đây là câu lệnh chuẩn nhất để tạo một dự án Laravel mới, trong đó Composer sẽ kết nối với kho lưu trữ, tải về mã nguồn và tự động cài đặt các dependencies.

Nếu bạn đang thắc mắc học Laravel bắt đầu từ đâu, thì đây chính là vạch xuất phát. Mở Terminal, di chuyển đến thư mục bạn muốn lưu dự án (ví dụ: C:laragonwww) và gõ dòng lệnh sau: composer create-project laravel/laravel my-first-app

This command is the most popular, safe and stable way to install Laravel today. Make a cup of coffee and wait about 1-2 minutes. Composer will do all the heavy lifting. When the process is completed and the message "Application key set successfully" is displayed, you officially own a complete first Laravel project on your computer.

Khởi động server với Artisan: Lệnh php artisan serve và xem thành quả

Lệnh php artisan serve khởi động một máy chủ phát triển cục bộ (local development server) tích hợp sẵn trong PHP, giúp bạn chạy ứng dụng Laravel ngay lập tức mà không cần cấu hình phức tạp.

Tiếp theo, hãy di chuyển vào thư mục dự án vừa tạo bằng lệnh cd my-first-app. Sau đó, gõ lệnh php artisan serve. Bạn sẽ thấy một thông báo cung cấp đường dẫn truy cập, thông thường mặc định sẽ là http://localhost:8000 hoặc http://127.0.0.1:8000.

Copy this link and paste it into your web browser. Congratulation! Laravel's beautiful welcome interface appears. This is an important milestone in the Laravel programming tutorial process. It proves your environment is properly set up and ready for coding.

Laravel folder structure - Where to look to know which files do what?

Cấu trúc thư mục của Laravel được tổ chức rất khoa học, trong đó thư mục app chứa logic cốt lõi, routes chứa các định tuyến, resources/views chứa giao diện và public là nơi tiếp nhận mọi yêu cầu HTTP.

When you open a project in an IDE (like VS Code or PhpStorm), you will see a lot of folders. Don't panic! With Basic Laravel, you only need to focus on the following few main folders:

Directory Name Main Function
app/Http/Controllers Place where Controller files handle business logic are stored.
routes/web.php Where to declare and define website URL paths.
resources/views Place to store HTML interface files combined with Blade Template.

Nếu sau này bạn muốn triển khai ứng dụng web lên một máy chủ Linux thực tế, việc nắm vững Quản trị Linux server cơ bản cho developer sẽ giúp bạn tự tin cấu hình Laravel, đặc biệt là việc trỏ document root của web server vào thư mục public để đảm bảo an toàn.

Learn the "power trio" - MVC model in Laravel

MVC (Model-View-Controller) là một mẫu kiến trúc phần mềm chia ứng dụng thành 3 thành phần chính, giúp tách biệt logic xử lý dữ liệu, giao diện người dùng và luồng điều khiển.

Route (web.php) - Router for every request

Routing trong Laravel đóng vai trò tiếp nhận các yêu cầu HTTP từ người dùng và điều hướng chúng đến đúng Controller hoặc View tương ứng để xử lý.

Trong mô hình MVC trong Laravel, Route không hẳn là một chữ cái trong MVC, nhưng nó là người gác cổng tối cao. Mọi HTTP request gửi đến ứng dụng đều phải đi qua file routes/web.php.

Ví dụ, khi người dùng truy cập yourdomain.com/about, Route sẽ bắt lấy URI /about này và quyết định xem sẽ làm gì tiếp theo. Nó có thể trả về một chuỗi văn bản đơn giản hoặc gọi một phương thức trong Controller để xử lý logic phức tạp hơn. Việc quản lý định tuyến rõ ràng và rành mạch là một trong những điểm mạnh tuyệt đối của PHP framework này.

Controller - "The unit" that processes logic and data

Controller đóng vai trò là cầu nối giữa Model và View, nhận dữ liệu đầu vào từ người dùng, tương tác với Model để xử lý nghiệp vụ và trả kết quả về cho View.

When a Route receives a request, it usually assigns work to the Controller. Here, you will write object-oriented programming (OOP) code to handle logic. For example, get a product list from the database, check user access rights, or calculate the total shopping cart...

Controller is the coreBackend part of the system. If you are planning to expand the system to mobile platforms, knowing how to Create a backend API with AI in 10 minutes can combine perfectly with Controllers in Laravel to create RESTful APIs that return JSON data quickly and professionally.

View (Blade Template) - "Representative face" of the application

View là phần giao diện hiển thị cho người dùng, và Laravel sử dụng Blade Template Engine để giúp việc viết HTML kết hợp với mã PHP trở nên gọn gàng và an toàn hơn.

Nếu Controller là "bộ não", thì View chính là "khuôn mặt" thuộc về phía Frontend. Blade Template Engine của Laravel cực kỳ thông minh và mạnh mẽ. Nó cho phép bạn sử dụng các cấu trúc điều khiển như vòng lặp @foreach, câu lệnh @if ngay trong file HTML một cách tự nhiên mà không làm rối code.

Điều tuyệt vời là Blade tự động biên dịch các file .blade.php thành mã PHP thuần và được cache lại, mang đến tốc độ tải trang cực nhanh. Nó cũng tự động mã hóa dữ liệu đầu ra để bảo vệ ứng dụng khỏi các cuộc tấn công XSS (Cross-Site Scripting).

Other core concepts and tools to know

Bên cạnh kiến trúc MVC, Laravel còn cung cấp các công cụ mạnh mẽ như Artisan CLI để tự động hóa, Eloquent ORM để thao tác database và Migration để quản lý cấu trúc cơ sở dữ liệu.

Artisan CLI - A powerful assistant for programmers

Artisan là giao diện dòng lệnh (Command Line Interface) tích hợp sẵn trong Laravel, cung cấp hàng chục lệnh hữu ích để tạo file, chạy migration và quản lý ứng dụng.

Bạn đã dùng php artisan serve ở phần trên rồi đấy! Artisan CLI có thể làm được nhiều hơn thế rất nhiều. Thay vì tạo file Controller bằng tay và phải tự gõ các namespace, bạn chỉ cần gõ php artisan make:controller UserController.

It saves you tons of time and ensures files are created properly. Any quality Vietnamese Laravel tutorial will emphasize mastering the use of Artisan to speed up project development.

Eloquent ORM - Working with databases has never been easier

Eloquent ORM là công cụ ánh xạ đối tượng-quan hệ (Object-Relational Mapping) của Laravel, cho phép bạn tương tác với cơ sở dữ liệu thông qua các Model thay vì phải viết SQL thuần.

Hãy quên đi những câu lệnh SELECT * FROM users WHERE active = 1 dài dòng. Với Eloquent ORM, bạn chỉ cần viết User::where('active', 1)->get(). Thật sự vi diệu! Nó biến mỗi bảng trong cơ sở dữ liệu thành một "Model" (đối tượng) trong PHP.

Nhờ đó, việc thêm, sửa, xóa dữ liệu trở nên trực quan và mang đậm tính lập trình hướng đối tượng. Điều này giúp mã nguồn sạch sẽ, dễ bảo trì và đặc biệt hữu ích khi bạn muốn thay đổi loại hệ quản trị cơ sở dữ liệu mà không cần viết lại toàn bộ query.

Migration - Manage database versions professionally

Migration giống như hệ thống quản lý phiên bản (như Git) dành cho cơ sở dữ liệu, cho phép team phát triển cùng chia sẻ và sửa đổi cấu trúc database một cách đồng bộ.

If you work in a group, someone adding a column to the database and then forgetting to tell someone else is a disaster. Migration completely solves this problem. You define the table structure (create columns, data types) with PHP code through migration files.

Khi cần triển khai ứng dụng lên các nền tảng đám mây, việc tìm hiểu DigitalOcean vs Vultr vs Linode so sánh sẽ giúp bạn chọn được máy chủ tốt với chi phí hợp lý. Và khi đó, chỉ cần một lệnh php artisan migrate, toàn bộ cấu trúc database sẽ được tạo mới hoàn hảo trên server. Nếu bạn là một freelancer quản lý nhiều dự án, việc Tự setup VPS chạy nhiều website và chạy migration cho từng site độc lập sẽ giúp quy trình deploy trở nên trơn tru và cực kỳ chuyên nghiệp.

With experience implementing hundreds of projects, our Pham Hai team recommends that you get into the habit of using Migration right from the smallest projects to create a good thinking foundation for the future.

So you've gone through the most important first steps on your journey with Laravel. From preparing the environment, successfully installing the project to understanding how core components work together. Don't stop here, this is just the beginning. The real power of Laravel framework PHP tutorial from scratch also lies in the vast ecosystem and amazing support community you're about to discover for yourself.

A little challenge for you: Try creating a new Route that returns a View with the text "I did it!". If you have any questions during the practice process, don't hesitate to leave a comment below, I will respond as soon as possible.

Note: The information in this article is for reference only. For the best advice, please contact us directly for specific advice based on your actual needs.

Categories: Lập Trình Web PHP

mrhai

Để lại bình luận