@routes Blade directive.
The ziggy:generate Command
Generate Ziggy’s configuration file by running:resources/js/ziggy.js containing all your application’s named routes and configuration.
Generated File Structure
The generated file looks like this:resources/js/ziggy.js
Customizing the Output Path
You can specify a custom output path in three ways:1. Command Argument
2. Config File
Set the default path inconfig/ziggy.php:
config/ziggy.php
3. Directory Output
If you specify a directory, Ziggy will create a file namedziggy.js inside it:
Command Options
Filtering Routes
You can filter which routes are included using command-line options:only or except settings in your config file.
Using Groups
Generate routes for a specific group:config/ziggy.php:
config/ziggy.php
Custom URL
Override the application URL in the generated config:TypeScript Support
Generate TypeScript declaration files for route name autocomplete:When to Regenerate
You need to regenerate the Ziggy config file whenever your routes change. This includes:- Adding new routes
- Removing routes
- Renaming routes
- Changing route parameters
- Modifying route URIs
- Changing route middleware (if you’re including middleware in the output)
Automatic Regeneration
You can automate regeneration when route files change using build tool plugins or file watchers.Vite Plugin
Use vite-plugin-ziggy:vite.config.js
Custom File Watcher
Create a file watcher that regenerates Ziggy when route files change:watch-routes.js
package.json
Laravel Mix Plugin
Show Laravel Mix plugin example
Show Laravel Mix plugin example
webpack.mix.js
Importing the Generated Config
Once generated, import the Ziggy config in your JavaScript:app.js
Version Control
Whether to commit the generatedziggy.js file depends on your workflow:
Commit the file if:
- You want to avoid build steps for contributors
- Your CI/CD doesn’t run
ziggy:generate - You want to track route changes in version control
- Team members work without running Laravel locally
Don’t commit the file if:
- You have automated regeneration in your build process
- You prefer to keep generated files out of version control
- Your deployment process runs
ziggy:generate - The file changes frequently and creates merge conflicts
If you don’t commit the file, add it to
.gitignore:Configuration Options
Customize the output format and behavior inconfig/ziggy.php:
config/ziggy.php
What's the difference between the @routes directive and ziggy:generate?
What's the difference between the @routes directive and ziggy:generate?
The
@routes Blade directive embeds routes directly in your HTML as a <script> tag. This works great for traditional Laravel apps with Blade templates.The ziggy:generate command creates a separate JavaScript file that you import. This is better for:- JavaScript frameworks (Vue, React, Svelte, etc.)
- SPAs with separate frontend repositories
- Apps that use a build tool (Vite, Webpack, etc.)
- When you want better separation of concerns
How does ziggy:generate handle route caching?
How does ziggy:generate handle route caching?
The
ziggy:generate command reads routes from Laravel’s route collection at runtime. If you’re using Laravel’s route caching (php artisan route:cache), Ziggy will use the cached routes.The Ziggy class caches the compiled route list internally during the same request, but this cache is cleared between command runs.Can I generate different files for different environments?
Can I generate different files for different environments?
Yes, you can use the Then import the appropriate file based on your environment.
--url option to generate different configs for different environments:Next Steps
Blade Directive
Learn about the @routes directive for Blade templates
CSP Configuration
Configure Content Security Policy with Ziggy