Installation
Ziggy can be installed in any Laravel application using Composer. For SPAs or applications with separate frontend repositories, you can also install the NPM package.Requirements
- PHP: 8.1 or higher
- Laravel: 9.0 or higher
- JSON extension: Required for PHP
Composer Installation
Install Ziggy via Composer in your Laravel application:No additional configuration is required. Ziggy is ready to use immediately after installation.
NPM Installation (Optional)
For applications using JavaScript bundlers like Vite or Webpack, you can optionally install Ziggy’s NPM package:The NPM package is optional for most Laravel applications. You only need it if you’re:
- Building an SPA with a separate frontend repository
- Not using the
@routesBlade directive - Importing Ziggy directly in JavaScript modules
Basic Setup
Adding the @routes Directive
The simplest way to use Ziggy is to add the@routes Blade directive to your main layout file. This makes the route() function available globally in your JavaScript.
Add the directive in your layout’s <head> section, before your application’s JavaScript:
What @routes Does
The@routes directive outputs:
- Your application’s base URL and configuration
- A list of all named routes with their URIs, methods, and parameters
- The Ziggy JavaScript
route()helper function
Verifying Installation
If
route('test') returns a URL, Ziggy is installed correctly!Configuration (Optional)
Ziggy works out of the box without any configuration. However, you can customize its behavior by publishing the configuration file:config/ziggy.php file with the following options:
config/ziggy.php
Configuration Options Explained
Configuration Options Explained
only: Array of route patterns to include (e.g., ['posts.*', 'users.show'])except: Array of route patterns to exclude (e.g., ['admin.*', '_debugbar.*'])groups: Named groups of routes that can be included on specific pagesoutput.path: Where to generate the routes file when using php artisan ziggy:generateAlternative Setup Methods
For JavaScript Frameworks (Vue, React)
If you’re building an SPA or prefer to import Ziggy in your JavaScript modules:Generate the Routes File
Run the Artisan command to generate a JavaScript routes file:This creates
resources/js/ziggy.js with your route configuration.For SPAs or Separate Repositories
If your frontend is completely separate from your Laravel backend:Generate Routes File
Run the command in your Laravel app:Copy the generated file to your frontend project.
TypeScript Support
Ziggy includes TypeScript type definitions. To enable route name autocompletion:.d.ts file:
types/ziggy.d.ts
Next Steps
Quickstart Guide
Build your first Ziggy-powered feature in minutes
Usage Examples
Learn all the ways to use the route() function