Introduction to Ziggy
Ziggy is a Laravel package that makes your named routes available in JavaScript, providing a familiarroute() helper function that mirrors Laravel’s PHP API.
What is Ziggy?
Ziggy bridges the gap between your Laravel backend and JavaScript frontend by automatically exposing your application’s named routes to JavaScript. Instead of hardcoding URLs in your JavaScript code, you can use the same convenientroute() helper you’re familiar with from Laravel.
Why Use Ziggy?
Type-Safe Routes
Catch broken route references at development time instead of runtime. Ziggy includes TypeScript support with route name and parameter autocompletion.
DRY Principle
Define your routes once in Laravel and use them everywhere. No need to maintain duplicate route definitions in JavaScript.
Automatic Updates
When you change a route in Laravel, your JavaScript automatically gets the update. No manual synchronization required.
Framework Agnostic
Works seamlessly with Vue, React, or vanilla JavaScript. Includes dedicated plugins and hooks for popular frameworks.
Key Features
Route Parameters
Ziggy handles all types of route parameters, from simple to complex:Route Model Binding
Ziggy understands Laravel’s route model binding and automatically uses the correct key:Current Route Detection
Check which route you’re currently on:Route Parameter Access
Access parameters from the current URL:Comparison with Alternatives
Hardcoded URLs
Hardcoded URLs
Without Ziggy:Problem: Routes break when URLs change, no validation, easy to make typosWith Ziggy:Benefit: Type-safe, validated, updates automatically
Manual Route Files
Manual Route Files
Without Ziggy:
Maintain a separate
routes.js file that duplicates your Laravel routesProblem: Double maintenance, routes get out of sync, manual updates requiredWith Ziggy:
Routes are automatically generated from your Laravel applicationBenefit: Single source of truth, zero maintenanceTemplate Variables
Template Variables
Without Ziggy:
Pass individual URLs from Blade to JavaScriptProblem: Requires passing every URL you need, clutters templatesWith Ziggy:
All routes available via single
@routes directiveBenefit: Clean templates, all routes available everywhereHow It Works
Ziggy works in three simple steps:Add @routes Directive
Include Ziggy’s Blade directive in your layout:This makes all your routes available to JavaScript.
Security Considerations
Ziggy includes powerful filtering options to control which routes are exposed:- Route filtering: Use
onlyandexceptin your config - Route groups: Define groups of routes for different pages
- Per-page control: Pass group names to
@routesdirective
Filtering routes in Ziggy is not a security feature. Always protect sensitive routes with proper authentication and authorization, regardless of whether they appear in Ziggy’s output.
Getting Started
Ready to start using Ziggy? Follow the installation guide to get set up in minutes.Installation
Install Ziggy and add the @routes directive to your application