404 Not Found

404 Not Found


nginx

Introduction to Laravel and Setting Up the Development Environment

Laravel is the world's most popular PHP framework—its elegant syntax makes web development efficient and enjoyable, much like the evolution from handwritten letters to email.

1. What You'll Learn


2. A True Story of an Entrepreneur

(1) Pain Point: Manually maintaining e-commerce analytics scripts

Bob is an independent developer who manually maintains more than 20 PHP analytics scripts for five e-commerce clients—every time a client requests a new report, he has to write SQL from scratch, piece together HTML, and handle file uploads. Last month, Alice's store needed to add a subscription billing feature. It took Bob three all-nighters to deliver it, and the code was scattered across 15 folders—even he couldn't figure out where the logic was. Charlie had it even worse: he tried writing an API in native PHP, only to find that all 2,000 lines of code were riddled with SQL injection vulnerabilities.

(2) Laravel's Solution

Laravel uses a set of elegant conventions to encapsulate repetitive tasks—such as routing, databases, authentication, and queues—into ready-to-use tools. Bob simply needs to define his models and routes, and CRUD pages, API endpoints, and user authentication will all be generated automatically.

BASH
# Create project in 30 seconds
composer create-project laravel/laravel ShopMetrics
cd ShopMetrics
php artisan serve
# Visit http://localhost:8000 — your app is running!

(3) Revenue

After Bob rewrote ShopMetrics using Laravel, the time required to generate a new data report was reduced from 3 days to 3 hours, and the amount of code was cut by 70%; Alice's subscription feature went live in just 2 days.


3. What Is Laravel?

Laravel is an open-source PHP web framework created by Taylor Otwell in 2011. It follows the MVC architectural pattern and is known for its "elegant syntax" and "developer experience."

100%
graph LR
    A[Laravel Framework] --> B[Routing]
    A --> C[Eloquent ORM]
    A --> D[Blade Templates]
    A --> E[Authentication]
    A --> F[Queue System]
    A --> G[Event Broadcasting]
    A --> H[Artisan Console]

(1) Core Design Philosophy

(2) New Features in Laravel 11

Feature Description
Streamline the directory structure Remove 9 files, including app/Http/Kernel.php
A Lighter-Weight Bootstrap bootstrap/app.php Configuring Middleware and Exceptions in a Single File
Uses SQLite by default Ready to use right out of the box, no MySQL configuration required
Per-Second Rate Limiting Rate Limiting Accurate to the Second
Scout Search Improvements Support for full-text search and vector search

4. An Overview of the Laravel Ecosystem

Laravel not only offers a core framework but also a complete set of official tools that cover the entire process from development to deployment.

Tool Purpose Description
Forge Server Management One-click deployment to DigitalOcean/Vultr
Vapor Serverless Deployment Serverless Solution Based on AWS Lambda
Nova Backend Panel Official CMS/Admin Panel
Sail Docker Development Environment Built-in Docker Compose Configuration
Herd Local Development Environment Native PHP Runtime for macOS/Windows
Breeze Authenticated Scaffolding Lightweight Login/Registration Template
Sanctum API Authentication SPA + Token Dual-Mode Authentication
Echo Real-time Broadcasting WebSocket Event Push Frontend Library

(1) ▶ Example: Checking the Laravel version and ecosystem tools

BASH
# Check Laravel version
php artisan --version
# Output: Laravel Framework 11.x

# Check installed packages via Composer
composer show | grep laravel
# laravel/framework
# laravel/sail
# laravel/sanctum
# laravel/tinker

Output:

TEXT
# Command executed successfully

5. Setting Up the Environment

(1) PHP 8.2+ and Composer

Laravel 11 requires PHP 8.2 or later. Composer is a package manager for PHP, similar to npm for Node.js.

Requirements Minimum Version Recommended Version
PHP 8.2 8.3
Composer 2.0 2.8+
Extension: Ctype
Extension: cURL
Extensions: DOM
Extension: MBstring
Extension: OpenSSL
Extension: PDO
Extension: Tokenizer

(1) ▶ Example: Install Composer and Verify the Environment

BASH
# Install Composer (macOS/Linux)
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

# Verify PHP and Composer versions
php -v
# PHP 8.3.x (cli)

composer -V
# Composer version 2.8.x

Output:

TEXT
{"status":"ok","data":{}}

(2) Docker Solutions: Laravel Sail vs. Laravel Herd

Dimension Laravel Sail Laravel Herd
Principles Docker Containers Native PHP + Nginx
Platform Win/Mac/Linux Mac/Windows
Performance Slower (container overhead) Faster (runs natively)
Isolation Full Isolation Partial Isolation
Suitable for Unified team environment Rapid individual development
Startup Time 30-60 s 5 s

(2) ▶ Example: Starting a Project Using Laravel Sail

BASH
# Create project with Sail
curl -s https://laravel.build/ShopMetrics | bash

cd ShopMetrics

# Start all services (MySQL, Redis, MeiliSearch...)
./vendor/bin/sail up -d

# Run Artisan commands inside Sail
./vendor/bin/sail artisan migrate

Output:

TEXT
{"status":"ok","data":{}}

(3) ▶ Example: Starting a Project Using Laravel Herd

BASH
# After installing Herd from laravelherd.com
# Herd auto-detects projects in ~/Herd or ~/Sites

cd ~/Herd
composer create-project laravel/laravel ShopMetrics
cd ShopMetrics

# Herd auto-provisions .test domain
# Visit: http://ShopMetrics.test

Output:

TEXT
# Command executed successfully

6. Create a ShopMetrics Project

(1) Creating a Project Using Composer

BASH
composer create-project laravel/laravel ShopMetrics
cd ShopMetrics

(2) Analyzing the Directory Structure

TEXT
ShopMetrics/
├── app/                  # Application core code
│   ├── Http/             # Controllers, Middleware, Requests
│   ├── Models/           # Eloquent ORM models
│   └── Providers/        # Service providers
├── bootstrap/            # Framework bootstrap files
├── config/               # Configuration files
├── database/             # Migrations, Seeders, Factories
├── public/               # Web root (index.php)
├── resources/            # Views, assets, language files
├── routes/               # Route definitions
│   ├── web.php           # Web routes
│   └── api.php           # API routes
├── storage/              # Logs, cache, compiled files
├── tests/                # PHPUnit/Pest tests
└── vendor/               # Composer dependencies
Table of Contents Responsibilities Analogies
app/ Business Logic The Company's Office
routes/ URL Routing Company Receptionist
resources/views/ Page Template Company Showroom
database/ Data Structures The Company's Archives
config/ Configuration Parameters Company Policies and Procedures
public/ Web Portal The Company's Gateway

(1) ▶ Example: Start the development server and access the home page

BASH
cd ShopMetrics

# Start the built-in development server
php artisan serve
# Starting Laravel development server: http://127.0.0.1:8000

# Visit http://127.0.0.1:8000 in browser
# You should see the Laravel welcome page

Output:

TEXT
# Command executed successfully

7. Comprehensive Example: Initializing the ShopMetrics Project

BASH
# ============================================
# Comprehensive: Initialize ShopMetrics project
# Covers: create project, configure .env, run migrations, serve
# ============================================

# 1. Create the Laravel project
composer create-project laravel/laravel ShopMetrics
cd ShopMetrics

# 2. Configure environment
cp .env.example .env
php artisan key:generate
# Application key set successfully.

# 3. Configure database in .env (using SQLite for quick start)
# DB_CONNECTION=sqlite
# DB_DATABASE=database/database.sqlite

# 4. Run default migrations
php artisan migrate
# Created migration tables: users, password_reset_tokens, etc.

# 5. Start development server
php artisan serve
# Server running on http://127.0.0.1:8000

# 6. Verify installation
php artisan about
# Environment: Local
# Laravel Version: 11.x
# PHP Version: 8.3.x

Output:

TEXT
Application key set successfully.
Info: Using SQLite database: database/database.sqlite
Migration table created successfully.
Running migrations:
  0001_01_01_000000_create_users_table ... done
  0001_01_01_000001_create_password_reset_tokens_table ... done
Starting Laravel development server: http://127.0.0.1:8000

❓ FAQ

Q What is the difference between Laravel and CodeIgniter?
A Laravel is a full-stack framework with built-in ORM, authentication, queues, and broadcasts; CodeIgniter is a lightweight framework that only provides routing and basic MVC, while other features must be implemented manually. Laravel is suitable for medium to large projects, while CodeIgniter is suitable for small projects.
Q Do I have to use Docker?
A No, you don't have to. Docker (Sail) is suitable for teams looking to standardize their environment, while Laravel Herd is ideal for individuals seeking rapid development. If you already have a PHP 8.2+ environment, you can simply run php artisan serve.
Q Are there significant differences between Laravel 11 and Laravel 10?
A Laravel 11 streamlines the directory structure and startup process, but the core API remains largely unchanged. Upgrading from Laravel 10 typically only requires adjusting the bootstrap/app.php configuration.
Q Why is SQLite recommended as a database for beginners?
A SQLite requires no installation—unlike MySQL or PostgreSQL—and is ready to use right out of the box, making it ideal for learning and prototyping. For production environments, we recommend switching to MySQL or PostgreSQL.
Q What is the difference between Composer and npm?
A Composer is the package manager for PHP (manages the vendor/ directory), while npm is the package manager for Node.js (manages the node_modules/ directory). Laravel uses both: Composer manages PHP dependencies, and npm manages front-end resources.
Q How can I determine if my local environment meets Laravel's requirements?
A Run php artisan about in the project directory. It will list all environment information, such as the PHP version, extensions, and database drivers; any items that do not meet the requirements will be highlighted in red.

📖 Summary


📝 Exercises

  1. Basic Exercise (⭐): Install PHP 8.2+ and Composer locally, and use composer create-project to create a Laravel project named ShopMetrics. Make sure you can view the welcome page in your browser.

  2. Advanced Exercise (⭐⭐): Create the same project using either Laravel Sail or Docker, compare the differences between the php artisan serve and Sail startup methods, and record the startup time and memory usage.

  3. Challenge (⭐⭐⭐): Review the changes to the Laravel 11 directory structure (compared to Laravel 10), and list the files that have been removed along with how their functionality is now implemented in Laravel 11.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏