Skip to main content

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:
By default, this creates resources/js/ziggy.js. You can customize the output path:
The generated file exports a 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:
Now you can import Ziggy with a clean import path:

Using the NPM Package

For SPAs or separate repositories without a vendor directory, install Ziggy from NPM:
Then import it directly:
You’ll need to make your generated ziggy.js file available to your frontend, either by:
  • Running php artisan ziggy:generate and 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:
Then in your frontend:

Auto-Regenerating Routes

When using ziggy:generate, you may want to automatically regenerate the file when routes change. Here’s a Vite plugin example:
For more examples, see vite-plugin-ziggy.

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