@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
<script> tag containing both Ziggy’s route configuration and the route() helper function.
Parameters
Filter routes by group name(s). Groups are defined in your
config/ziggy.php file.Add a nonce attribute to the generated script tag for Content Security Policy (CSP) compliance.Output:
Output routes as JSON instead of JavaScript. Useful when using CSP or when you want to load the When
route() function separately.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
Withjson: 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:
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 inconfig/ziggy.php:
Implementation Details
The@routes directive is powered by the BladeRouteGenerator class, which:
- Creates a new
Ziggyinstance with the specified group filter - Applies route filtering from your configuration
- Outputs the appropriate format (script, JSON, or merge script)
- Adds the nonce attribute if provided
- Includes or excludes the
route()function based on theskip-route-functionconfig
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.Related
Ziggy Class
Learn about the Ziggy class that powers the @routes directive
Configuration
Configure route filtering and output options