Overview
Laravel Breeze API + Next.js is a modern full-stack application that separates concerns between a robust Laravel backend API and a dynamic Next.js frontend. This architecture provides a clear separation between the presentation layer and business logic, enabling independent scaling and deployment strategies.Architecture Components
Backend: Laravel API
The backend is built with Laravel 12 and serves as a RESTful API. It handles:- Authentication & Authorization via Laravel Sanctum
- Business Logic through controllers and services
- Data Persistence using Eloquent ORM
- Request Validation via Form Requests
- Background Jobs through Laravel Queue
Frontend: Next.js Application
The frontend is built with Next.js 16, React 18, and TypeScript. It provides:- Server-Side Rendering (SSR) for optimal performance
- Client-Side Routing via Next.js App Router
- State Management using SWR for data fetching
- Type Safety through TypeScript
- Modern UI with Tailwind CSS
Communication Flow
Request/Response Cycle
The frontend and backend communicate via HTTP requests using Axios:Frontend Initiates Request
The Next.js application sends an HTTP request using the configured Axios instance.
Frontend/src/lib/axios.ts
CORS Middleware Validation
Laravel’s CORS middleware validates the request origin and allows cross-origin requests from the frontend.
Route Resolution
Laravel routes the request to the appropriate controller based on the endpoint.
Backend/routes/api.php
Controller Processing
The controller processes the request, interacts with models, and prepares the response.
CORS Configuration
Cross-Origin Resource Sharing (CORS) is configured to allow the Next.js frontend to communicate with the Laravel backend:Backend/config/cors.php
The
supports_credentials: true setting is crucial for cookie-based authentication with Laravel Sanctum.Environment Configuration
Backend Environment Variables
Key environment variables for the Laravel backend:Frontend Environment Variables
Key environment variables for the Next.js frontend:Data Flow Diagram
API Structure
The API follows RESTful conventions:Authentication Routes
/register, /login, /logoutHandles user authentication flowsProtected API Routes
/api/*Requires authentication via SanctumPublic Routes
/sanctum/csrf-cookieCSRF token endpoint for SPA authenticationEmail Verification
/verify-email/*, /email/verification-notificationEmail verification flowsSecurity Considerations
CSRF Protection
CSRF Protection
Laravel’s CSRF protection is enabled for all state-changing requests. The frontend must request a CSRF token before submitting forms.
Cookie-Based Authentication
Cookie-Based Authentication
Rate Limiting
Rate Limiting
API endpoints are protected with rate limiting to prevent abuse.
HTTPS in Production
HTTPS in Production
Always use HTTPS in production to protect credentials and sensitive data in transit.
Deployment Considerations
Monorepo Structure
The project uses a monorepo structure with separateBackend/ and Frontend/ directories:
- Backend: Deploy to any PHP hosting (Laravel Forge, AWS, DigitalOcean)
- Frontend: Deploy to Vercel, Netlify, or any Node.js hosting
Environment Separation
Ensure environment variables are properly configured for each environment:The frontend and backend can be deployed to different domains, but ensure CORS is properly configured.
Next Steps
Authentication Flow
Learn how Laravel Sanctum handles authentication
Project Structure
Explore the directory structure and organization