Web routes handle requests that return HTML content, such as web pages, forms, and traditional server-rendered views.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MonishAMPT/fastroute-code/llms.txt
Use this file to discover all available pages before exploring further.
Basic Web Route Definition
Web routes are defined inroutes/web.php and return a function that accepts a RouteCollector instance:
routes/web.php
Creating Web Routes
Simple Page Routes
Define routes that render HTML views:routes/web.php
Web routes typically use inline closures that include or require view files from the
views/ directory.Organizing Views
Store your HTML templates in a dedicatedviews/ directory:
Example View File
views/main.php
Content-Type Headers
FastRoute automatically sets the correct content type based on the route path:index.php
Dynamic Routes with Parameters
You can create dynamic web routes with parameters:routes/web.php
views/blog.php
Handling Forms
Create routes for displaying and processing forms:routes/web.php
Route Groups for Web Pages
Organize related web pages using route groups:routes/web.php
Complete Example
Here’s the complete web routes file from the source code:routes/web.php
Best Practices
Keep Routes Simple
Keep Routes Simple
Use closures in web routes primarily for loading views. Move complex logic to controllers or handler classes.
Organize Views by Feature
Organize Views by Feature
Group related views in subdirectories like
views/blog/, views/admin/, etc.Use Layouts
Use Layouts
Create reusable layout files to avoid duplicating HTML structure across views.
Sanitize Output
Sanitize Output
Always use
htmlspecialchars() when outputting user data in views to prevent XSS attacks.Differences from API Routes
Content Type
Web routes return HTML (text/html), while API routes return JSON (application/json)
Response Format
Web routes use
require to include views, API routes use echo json_encode()File Organization
Web routes load from
views/, API routes typically call handlers or return dataUse Case
Web routes for browsers and HTML pages, API routes for AJAX, mobile apps, and integrations
Next Steps
Route Handlers
Learn about different handler types
API Routes
Create API endpoints that return JSON