Skip to main content
The @routes Blade directive outputs your Laravel routes as JavaScript, making them available to Ziggy’s route() helper function. Include it in your layout before your application’s JavaScript files.

Basic Usage

By default, this outputs a <script> tag containing both Ziggy’s route configuration and the route() helper function.

Parameters

string|array
Filter routes by group name(s). Groups are defined in your config/ziggy.php file.
string
Add a nonce attribute to the generated script tag for Content Security Policy (CSP) compliance.
Output:
boolean
default:false
Output routes as JSON instead of JavaScript. Useful when using CSP or when you want to load the route() function separately.
When json: true, the output is plain JSON without the route() helper function. You’ll need to import the route() function separately from ziggy-js.

Output Format

Default Output (JavaScript)

By default, @routes outputs a complete <script> tag:

JSON Output

With json: true, only the configuration is output:

Multiple @routes Directives

If you include @routes multiple times on the same page, only the first directive includes the full route() function. Subsequent directives output a merge script that combines their routes with the existing configuration:
This ensures the route() function is only loaded once while still allowing you to conditionally load different route groups.

Examples

Basic Usage

Filter by Group

Content Security Policy

Conditional Loading

Configuration

You can customize the output format by setting the output class in config/ziggy.php:

Implementation Details

The @routes directive is powered by the BladeRouteGenerator class, which:
  1. Creates a new Ziggy instance with the specified group filter
  2. Applies route filtering from your configuration
  3. Outputs the appropriate format (script, JSON, or merge script)
  4. Adds the nonce attribute if provided
  5. Includes or excludes the route() function based on the skip-route-function config
The @routes directive exposes route names and URIs in your page’s HTML source. Ensure you use proper authentication and authorization on your routes, and consider using route filtering to limit which routes are exposed.

Ziggy Class

Learn about the Ziggy class that powers the @routes directive

Configuration

Configure route filtering and output options