Configuration
To set up route filtering, create a config file in your Laravel app atconfig/ziggy.php and add either an only or except key containing an array of route name patterns.
You must choose one or the other. Setting both
only and except will disable filtering altogether and return all named routes.Including Routes with only
Use the only configuration to explicitly include specific routes:
config/ziggy.php
home, posts.index, and posts.show routes to your JavaScript.
Excluding Routes with except
Use the except configuration to exclude specific routes:
config/ziggy.php
_debugbar., horizon., or admin. from Ziggy’s output.
Wildcard Patterns
You can use asterisks as wildcards in route filters. The wildcard* matches any sequence of characters:
config/ziggy.php
admin.loginadmin.registeradmin.users.indexadmin.dashboard
Wildcard Examples
Filtering by Route Name
Ziggy’sfilter() method processes route names using Laravel’s Str::is() pattern matching. This means you have flexible pattern matching capabilities:
Negative Patterns
You can use negative patterns (prefixed with!) to exclude specific routes from a broader filter:
config/ziggy.php
posts.* routes except posts.destroy.
Negative Pattern Examples
Security Considerations
Best Practices
- Filter sensitive routes: Exclude admin, internal, or debugging routes from public pages
- Use route groups: Organize routes into logical groups for different parts of your application
- Protect with middleware: Always use Laravel’s authentication and authorization middleware
- Review regularly: Periodically audit which routes are exposed to the frontend
What happens if I set both 'only' and 'except'?
What happens if I set both 'only' and 'except'?
If you configure both Choose either
only and except in your config file, Ziggy will disable filtering altogether and return all named routes. This is intentional behavior to prevent confusion about filter precedence.only or except, but not both.Can I filter routes differently on different pages?
Can I filter routes differently on different pages?
Yes! You can use route groups to expose different sets of routes on different pages. Pass a group name to the
@routes directive:Do filters apply to the ziggy:generate command?
Do filters apply to the ziggy:generate command?
Yes, the
only and except config options apply to the ziggy:generate Artisan command. You can also override them using command-line options:Next Steps
Route Groups
Organize routes into groups for different pages
Generating Config
Generate Ziggy’s config as a file for JavaScript frameworks