Overview
Serving Ziggy’s configuration from an API endpoint provides several benefits:- Always up-to-date: No need to regenerate static files when routes change
- Dynamic filtering: Serve different routes based on authentication or user permissions
- Simplified deployment: No need to copy config files between projects
- Cache control: Implement custom caching strategies
Creating the Basic Endpoint
Add the API route
Create a route that returns a new This endpoint will be available at
Ziggy instance as JSON:/api/ziggy.Test the endpoint
Visit the endpoint in your browser or use curl:You should see a JSON response like:
Advanced Configurations
Filtering Routes
Serve only specific routes to your frontend:Using Groups
Serve different route groups based on user permissions:config/ziggy.php:
Adding Cache Headers
Implement caching to reduce server load:Authenticated Endpoint
Protect the endpoint with authentication:Frontend Integration
React Hook Example
Create a custom hook to fetch and manage Ziggy config:Vue Composable Example
Global Configuration Strategy
Load the config once and make it globally available:Performance Optimization
Server-Side Caching
Cache the Ziggy config to avoid regenerating it on every request:Client-Side Caching
Cache the config in localStorage:ETags for Cache Validation
Troubleshooting
404 Not Found
404 Not Found
Ensure the route is properly defined:Verify the route appears in the list. If not, clear the route cache:
CORS Errors
CORS Errors
If your frontend is on a different domain, configure CORS:
Empty Routes Object
Empty Routes Object
Check your route filtering configuration:
Authentication Required
Authentication Required
If using Sanctum or other authentication:
Security Considerations
- Filter sensitive routes: Never expose admin or internal routes to public endpoints
- Rate limiting: Apply rate limiting to prevent abuse
- Authentication: Consider requiring authentication for production apps
- CORS: Only allow trusted origins in production
Best Practices
- Cache aggressively: Route configurations rarely change, so cache liberally
- Use groups: Filter routes appropriately for different user types
- Version your endpoint: Consider versioning (e.g.,
/api/v1/ziggy) for future changes - Monitor performance: Track endpoint response times and cache hit rates
- Handle errors gracefully: Provide fallbacks if config loading fails