Ziggy class is the core of Ziggy’s PHP functionality. It collects, filters, and serializes your Laravel routes into a format that can be used by JavaScript.
Class Overview
Constructor
string|array|null
Filter routes by group name(s). Groups must be defined in
config/ziggy.php.string|null
Override the base URL for generated routes.Defaults to
url('/') from your Laravel configuration.Methods
filter()
Filter routes by name using pattern matching.array
required
Array of route name patterns. Supports wildcards (
*) and exclusion patterns (prefixed with !).bool
default:true
When
true, includes only routes matching the filters. When false, excludes routes matching the filters.self (chainable)
Pattern Matching
toArray()
Convert the Ziggy instance to an array.url- Base URLport- Port number (if not default)defaults- Default route parametersroutes- Filtered route collection
toJson()
Convert the Ziggy instance to JSON.int
default:0
JSON encoding options (same as
json_encode() flags).clearRoutes()
Clear the internal route cache. Useful in testing or when routes change at runtime.Route Data Structure
Each route in the output contains:Usage Examples
Basic Usage
Filtering Routes
Using Groups
API Endpoint
Custom Response
Blade Directive Implementation
The@routes directive internally uses the Ziggy class:
Testing
Route Model Binding
Ziggy automatically detects route model binding keys:Custom Binding Keys
Direct route bindings are also detected:Folio Support
Ziggy automatically includes Laravel Folio routes:Caching
The Ziggy class caches routes internally for performance:The cache is stored in a static property and persists only for the current request. It’s automatically cleared between requests.
Related
Blade Directive
Use @routes in Blade templates
Artisan Commands
Generate route files with ziggy:generate
Configuration
Configure filtering and output options