Overview
Many applications don’t need additional setup beyond the@routes Blade directive. However, if you prefer to avoid using @routes or are working in a build-tool environment, you can import Ziggy’s route() function and configuration directly into your JavaScript/TypeScript files.
This approach is ideal for:
- Single-page applications (SPAs)
- Applications with separate frontend and backend repositories
- Projects that prefer explicit imports over global variables
- Build processes that benefit from tree-shaking
Generating Ziggy’s Configuration
Ziggy provides an Artisan command to output your routes configuration to a file:resources/js/ziggy.js. You can customize the output path:
Ziggy configuration object:
Importing the route() Function
Without the @routes directive, you need to import the route() function and pass the Ziggy config manually.
Direct Import from Vendor
You can import directly from the vendor directory:Setting Up an Alias
Create an alias in your build configuration to simplify imports:- Vite
- Webpack
- Laravel Mix
Using the NPM Package
For SPAs or separate repositories without avendor directory, install Ziggy from NPM:
ziggy.js file available to your frontend, either by:
- Running
php artisan ziggy:generateand copying the file to your frontend project - Creating an API endpoint that returns Ziggy’s config as JSON (see below)
Retrieving Config from an API
For fully separate frontend applications, you can fetch Ziggy’s config over the network:Auto-Regenerating Routes
When usingziggy:generate, you may want to automatically regenerate the file when routes change. Here’s a Vite plugin example:
Next Steps
Vue Integration
Use Ziggy with Vue 2 and Vue 3
React Integration
Use Ziggy with React via the useRoute hook
TypeScript
Configure TypeScript types and autocompletion
Filtering Routes
Control which routes are exposed to the frontend