Does Laravel Work With Cloudflare Pages?
Laravel can be deployed to Cloudflare Pages, but only as a static site generator or API—full server-side rendering isn't natively supported.
Quick Facts
How Laravel Works With Cloudflare Pages
Laravel is a server-side PHP framework designed for traditional application servers, while Cloudflare Pages is a JAMstack platform optimized for static sites and serverless functions. The primary incompatibility stems from Laravel's reliance on PHP execution and server state, neither of which Cloudflare Pages natively supports. However, you have two viable integration paths: (1) Use Laravel as a static site generator via tools like Splade or Inertia for pre-rendering pages, then deploy the build output to Pages, or (2) Deploy your Laravel API separately to Cloudflare Workers or another PHP host, and serve a decoupled frontend (Vue/React) through Pages. The second approach is more common and maintains Laravel's backend robustness while leveraging Pages' global CDN for frontend delivery. You'll sacrifice some Laravel conveniences like server-side sessions and CSRF middleware, requiring token-based auth instead. Build times increase since you're pre-rendering or building a separate frontend application.
Best Use Cases
Deploy Laravel API to Workers + Frontend to Pages
composer create-project laravel/laravel api-app# wrangler.toml (for Laravel API on Workers)
name = "laravel-api"
main = "public/index.php"
compatibility_date = "2024-01-01"
[[env.production.routes]]
pattern = "api.example.com/*"
zone_name = "example.com"
# Build script for Pages frontend
#!/bin/bash
npm install
npm run build
# pages.json (Pages config)
{
"buildCommand": "npm run build",
"outputDirectory": "dist",
"envVars": {
"VITE_API_URL": "https://api.example.com"
}
}
# Frontend .env.production
VITE_API_URL=https://api.example.com
# Fetch from API in frontend
const response = await fetch(
`${import.meta.env.VITE_API_URL}/api/posts`
);Known Issues & Gotchas
Laravel's server-side rendering and session management don't work on Cloudflare Pages
Fix: Use token-based authentication (JWT), pre-render pages as static HTML, or move to a serverless PHP runtime like Workers
Build times can be lengthy if pre-rendering large Laravel applications with many routes
Fix: Use selective pre-rendering, implement on-demand ISR strategies, or split into API + frontend architecture
Laravel's `.env` secrets can accidentally be exposed in static builds
Fix: Never commit secrets, use environment variables in your build process, and validate before deploying
Database connections and server-side dependencies aren't available in the Pages environment
Fix: Query databases via API endpoints hosted elsewhere, or use Cloudflare's D1 database with Workers as an intermediary
Alternatives
- •Next.js deployed to Vercel—native Node.js support with database integration and API routes
- •Statamic or Craft CMS with headless API—Laravel-like PHP experience with built-in JAMstack support
- •Astro + Express/Laravel backend—Astro handles static generation while Express runs separately for dynamic content
Resources
Related Compatibility Guides
Explore more compatibility guides