PHP and Laravel

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

The reference material consulted while preparing these pages is listed in the section bibliography.

This section is about building web applications with PHP. It starts with what plain PHP gives you: how a request reaches a script, how to read input, escape output, keep sessions, accept uploads, query a database with PDO and avoid the common security mistakes. It then builds the same kind of application with Laravel, the most widely used PHP framework, from a first route to production. It is written for developers who know some PHP, or another web framework, and want to build and run current PHP web applications. The language itself (syntax, types, objects, the standard library, Composer and testing tools) is covered in the PHP Reference, which these pages link to rather than repeat.

The pages are written against Laravel 13.x (13.34.0) on PHP 8.3 to 8.5, checked on 2026-10-05. Laravel ships one major version a year, and each one gets bug fixes for 18 months and security fixes for two years. Laravel 13 was released on 2026-03-17 and requires PHP 8.3 or newer. On 2026-10-05, Laravel 12 receives security fixes only, and 11 and older receive none. Laravel 14 is expected in the first quarter of 2027. PHP itself supports each release for four years, and 8.3 is in security-only support until the end of 2027. The details are on What’s Changed and Upgrading.

The two running examples

The examples on these pages build two small, original applications in the same domain as the Django, FastAPI and NestJS sections of this site, so that the stacks can be compared:

  • Bookshelf Lite is a book catalogue in plain PHP with PDO, a front controller and no framework. The plain PHP pages build it so that you can see exactly what a framework takes over.

  • Bookshelf is the same catalogue in Laravel 13: books, authors, reviews and loans with Blade pages, search and pagination, a starter-kit login with policies for editing reviews, cover uploads to an S3-compatible disk, English and Spanish translations, a live feed of new reviews over Reverb (and a server-sent events variant), moderation mail and notifications on a queue, Redis caching, a versioned JSON API with Sanctum and a JSON:API variant, scheduled tasks, and a Docker and FrankenPHP deployment.

Every Laravel example was run in that application, and its test suite (109 Pest tests at the end of this section) covers the examples that have behaviour to check. The pages say where something could not be run in the environment used to write them.

Diagram of the Bookshelf application. API clients use the JSON API under /api/v1 and /api/v2 with Sanctum tokens, and the browser uses Blade pages, Livewire, Inertia with React and Echo. Bookshelf Lite, shown dashed, is the same catalogue in plain PHP and PDO. Inside the Laravel 13 application, the HTTP layer holds the web and API route files, middleware for authentication, request forgery, throttling and locale, thin controllers, Form Requests, policies and gates, and the session and Sanctum guards. The application and data layer holds actions such as AddBook and PostReview, the Eloquent models Book, Author, Review, Loan and User with their relationships, casts and scopes, events and listeners, queued jobs and the HTTP client for the catalogue. The output layer holds Blade components and layouts, Livewire components, Inertia pages in React, API resources and JSON:API, mail and notifications, broadcast events and English and Spanish translations. The application uses a database (PostgreSQL 17, SQLite in tests), Redis for cache, sessions, queues and locks, queue workers or Horizon, the scheduler run every minute, an S3-compatible disk for cover images, SMTP mail with Mailpit locally, an external catalogue API with retries, and Reverb, which pushes broadcasts to the browser’s Echo client over wss. Workers, the scheduler and Reverb run the same code base as separate processes.

New here? Read in this order

  1. Introduction and Architecture — how PHP serves the web, MVC and where Laravel stands among the frameworks.

  2. The PHP Request Model and Servers, then the plain PHP pages up to Web Security Fundamentals. Readers who already know PHP on the web can skip to the next step.

  3. Laravel Getting Started and the rest of Laravel foundations, in order.

  4. The HTTP layer, from Routing to Validation and Form Requests, and Blade Templates.

  5. Database and Query Builder through Eloquent Relationships.

  6. Authentication, Authorization: Gates and Policies and Testing.

  7. Queues and Jobs and Deployment, before the first release.

The other pages can be read as needed. Coming from an older book, tutorial or Laravel version? Start with What’s Changed and Upgrading. Every page ends with a section on where older material is outdated, and Best Practices collects the rules in one checklist.

mindmap root((PHP and Laravel)) Foundations Introduction and Architecture Plain PHP Request Model and Servers Requests and Responses Forms and Escaping Sessions and Cookies File Uploads PDO Authentication by hand Web Security Routing and Middleware by hand HTTP Clients and Email Laravel foundations Getting Started Structure and Configuration Request Lifecycle Container and Facades Artisan and Tinker HTTP layer Routing Middleware Controllers Requests and Responses Validation Sessions and Flash Data Errors and Logging Views and front end Blade Blade Components Vite Livewire Inertia and Starter Kits Database and Eloquent Query Builder Migrations and Factories Models Relationships Casts and Serialization Collections and Pagination Query Performance Redis, MongoDB, Search Security Authentication Gates and Policies Sanctum and Passport CSRF, Encryption, Hashing APIs and integrations API Resources HTTP Client AI SDK, MCP, Boost Application services Queues and Jobs Events Broadcasting and Reverb Scheduling Mail and Notifications Cache and Rate Limiting File Storage Localization Processes and Context Testing Testing Dusk Production Deployment Docker and Sail Octane Monitoring Packages and Ecosystem Upgrading Best Practices Cheat Sheet

What’s covered

Foundations

  • Introduction and Architecture — how PHP serves the web, MVC and the front controller, framework or library, Active Record and Data Mapper, the Symfony components Laravel is built from, Laravel among PHP frameworks and other stacks, and where WordPress and Drupal fit.

Web development in plain PHP

  • The PHP Request Model and Servers — the shared-nothing model, SAPIs, PHP-FPM behind Nginx, FrankenPHP and worker mode, OPcache and persistent connections.

  • HTTP Requests and Responses — superglobals, JSON, PUT and PATCH bodies, status codes and headers, output buffering, URL parsing with the URI extension, and PSR-7.

  • Forms, Validation and Output Escaping — Post/Redirect/Get, validating input, escaping for each context, why "sanitize on input" is wrong, and mail header injection.

  • Sessions and Cookies — how sessions work, the security settings, fixation and hijacking, locking, storage, flash messages, cookies and logging out.

  • File Uploads and Images — the upload settings, $_FILES, validating and storing uploads, serving downloads, and resizing with GD.

  • Databases with PDO — connecting, prepared statements and SQL injection, fetch modes, transactions, a repository, and mysqli.

  • Authentication Without a Framework — registration and login, throttling, "remember me", password reset and HTTP Basic authentication.

  • Web Security Fundamentals — the PHP Manual’s security chapter, the OWASP Top 10:2025 for PHP, CSRF, injection, SSRF, deserialization, misconfiguration, secrets, headers and patching.

  • Templating, Routing and Middleware by Hand — PHP as a template language, a front controller and router, PSR-7, PSR-15 and PSR-11, the same application in Slim 4, and why a full framework pays off.

  • HTTP Clients, APIs and Email — a small JSON API, cURL, stream contexts, PSR-18 clients and sending email.

Laravel foundations

HTTP layer

  • Routing — route files, parameters, model binding, groups, rate limiting, signed URLs, route caching and Folio.

  • Middleware — writing and registering middleware, the default groups, parameters and priority, terminable middleware, the #[Middleware] attribute and trusted proxies.

  • Controllers — basic, single-action and resource controllers, middleware and authorization attributes, injection, and thin controllers with action classes.

  • Requests, Input and Responses — input, files and cookies, every response type, redirects, downloads and streamed responses.

  • Validation and Form Requests — rules, errors and old input, Form Requests, rule objects, custom rules and Precognition.

  • Sessions, Cookies and Flash Data — drivers, flash data, regeneration, JSON serialization in Laravel 13, session blocking and cookies.

  • Error Handling and Logging — exceptions in bootstrap/app.php, error pages, debug output, log channels, context and Pail.

Views and the front end

  • Blade Templates — layouts, escaping and XSS, JSON for JavaScript, directives, fragments, view composers and page metadata.

  • Blade Components and Layouts — class and anonymous components, attributes and slots, layouts and component packages.

  • Vite and Frontend Assets — @vite, static assets, JavaScript frameworks, CSP nonces, choosing a front end and migrating from Mix.

  • Livewire — Livewire 4 components, wire:model, actions, forms, events, lazy loading, testing, Flux and Alpine.js.

  • Inertia and Starter Kits — Inertia 3 with React, props and shared data, forms and errors, SSR, testing, and the starter kits.

Database and Eloquent

Security

APIs and integrations

Application services

  • Queues and Jobs — drivers, job classes and attributes, workers, failures, transactions, unique jobs, chains and batches, Horizon and idempotency.

  • Events and Listeners — events, discovery, transactions, model and domain events, and when not to use one.

  • Broadcasting and Reverb — broadcast events, channel authorization, the protocol, Echo, Reverb in production and server-sent events.

  • Task Scheduling — the cron entry, schedules, overlaps and several servers, and testing.

  • Mail and Notifications — mailers, Mailables, queued and translated mail, previews, failover and notifications.

  • Cache and Rate Limiting — stores, remember and flexible, tags, atomic locks, failover and rate limiting.

  • File Storage and Images — disks, public and private files, S3, temporary URLs, images and testing.

  • Localization — translation files, choosing the locale and what Laravel does not translate.

  • Processes, Concurrency and Context — running processes, the Concurrency facade, Context and feature flags with Pennant.

Testing

  • Testing — the testing environment, Pest, parallel runs and coverage, HTTP and console tests, the database and the fakes.

  • Browser Testing with Dusk — Dusk and ChromeDriver, page objects, the Pest browser plugin and CI.

Production and evolution

  • Deployment — Nginx and PHP-FPM, optimize and OPcache, releases, long-running processes, FrankenPHP and managed platforms.

  • Docker and Sail — Sail for development, a production image, and web, worker and scheduler containers from one image.

  • Octane and Performance — Octane on FrankenPHP, what it gains, state leaks between requests, OPcache and measuring.

  • Monitoring and Debugging — Telescope, Pulse, Nightwatch, Pail, the exception page and Xdebug.

  • Package Development and Ecosystem — writing and testing a package, and the first-party and community ecosystem.

  • What’s Changed and Upgrading — releases 6 to 13, the support policy, old code on Laravel 13, the slim skeleton, upgrading and what older books get wrong.

  • Best Practices — the practices that still hold, in one checklist.

  • Cheat Sheet — a one-page summary of the section, with a downloadable PDF.

These pages link to existing material instead of repeating it, and cover only the PHP and Laravel side.

Topic Where it lives How this section relates

The PHP language, Composer, static analysis and PHPUnit/Pest

PHP Reference

Pages link there for syntax, types, objects, the standard library and tooling.

HTML, CSS, JavaScript and TypeScript

HTML & CSS Reference, JavaScript Development and TypeScript Reference

The Blade, Vite, Livewire and Inertia pages cover only the Laravel side.

Front-end frameworks and Tailwind CSS

React Reference, Vue.js Reference and Tailwind Reference

Inertia pages and the starter kits use React, Vue and Tailwind as they are documented there.

Accessibility, CORS and browser testing

Web Accessibility, What is CORS? and E2E Testing in Real Browsers

The security and Dusk pages show Laravel’s configuration and APIs.

The same ideas in other stacks

Django, ASP.NET Reference, Vaadin Reference, Python FastAPI, NestJS, SpringBoot Reference and Quarkus Reference

Introduction and Architecture compares them.

OAuth 2.0, OpenID Connect, PKCE and multi-factor authentication

OAuth Reference

The authentication and Sanctum/Passport pages cover only the Laravel packages.

Messaging, scheduling and resilience patterns

Messaging Reference, Scheduling and Resilience

The queues, scheduling, HTTP client and rate-limiting pages show the Laravel parts.

Containers, Kubernetes and cloud platforms

Docker, Kubernetes and Cloud

The Docker, Sail and deployment pages cover only what PHP and Laravel need.

SQL, query tuning, pagination and schema changes

SQL Reference, Diagnosing and Fixing Slow Queries, Pagination: Offset vs. Keyset and Evolving the Database Model

The PDO, query builder, migrations, N+1 and pagination pages cover the PHP side.

Redis, MongoDB, Elasticsearch and vector search

Redis Reference, MongoDB Reference, Elasticsearch Reference and Vector Databases & RAG

The cache and Redis, MongoDB and search pages show only the Laravel clients.

GraphQL

GraphQL Reference

The API page mentions Lighthouse for GraphQL in Laravel.

MCP, agents and AI-assisted development

Model Context Protocol (MCP), AI Agents, AI-Assisted Development and RAG Systems in Production

The AI SDK, MCP and Boost page covers only the Laravel APIs.

The open issues for a comparison of backend and web frameworks (#263), of programming languages (#196) and of cloud providers (#201), and a future Symfony section, will add pages that this section will link to once they exist.

Bibliography

The reference material consulted while preparing this section. The PHP Manual, the Laravel documentation and the official documentation of each tool are the primary sources; the books are consulted references, not the primary source. On any discrepancy, or wherever a book is silent on a later feature, the official documentation wins. No text, code listing or figure is copied from any book.

Books

The Laravel documentation (laravel.com/framework/docs/13.x), grouped as the documentation is:

Laravel project and first-party products

The PHP Manual (php.net/manual), as the manual is organised:

php.net and the PHP project

  • Supported Versions.

  • The RFCs cited on individual pages, in the PHP RFC wiki: session security defaults, partitioned cookies (CHIPS), the bcrypt cost increase, FILTER_THROW_ON_FAILURE and persistent cURL share handles.

  • The PHP Internals Book on the PHP lifecycle.

PHP libraries, components and tooling

Servers, databases and infrastructure

Specifications and standards