Quickstart Guide
This guide will walk you through creating your first Ziggy-powered feature from a fresh Laravel installation to making your firstroute() call in JavaScript.
Prerequisites
Before starting, ensure you have:- Laravel 9.0 or higher installed
- PHP 8.1 or higher
- Node.js and npm (for frontend assets)
Step 1: Install Ziggy
Install Ziggy via Composer:Ziggy is now installed and automatically registered!
Step 2: Create Laravel Routes
Let’s create a simple blog post feature. Add these routes toroutes/web.php:
routes/web.php
The
.name() method is crucial—Ziggy only works with named routes.Step 3: Add @routes to Your Layout
Open your main layout file (typicallyresources/views/layouts/app.blade.php or create it if it doesn’t exist):
resources/views/layouts/app.blade.php
Step 4: Create a View with JavaScript
Create a posts listing page atresources/views/posts/index.blade.php:
resources/views/posts/index.blade.php
Step 5: Use route() in Your JavaScript
Let’s create a more complex example with Axios inresources/js/app.js:
resources/js/app.js
Step 6: Test It Out
1
Start Your Development Server
2
Open Browser Console
Visit
http://localhost:8000 and open your browser’s developer console (F12).3
Try Ziggy Commands
Test these commands in the console:
Success! You’re now using Ziggy to generate type-safe URLs in JavaScript.
Common Use Cases
With Alpine.js
With Vue 3
First, install the Vue plugin:resources/js/app.js
PostsList.vue
With React
PostsList.jsx
With Form Submissions
Debugging Tips
route() is not defined
route() is not defined
Problem:
Uncaught ReferenceError: route is not definedSolution:- Ensure
@routesis added to your layout before your JavaScript - Clear your view cache:
php artisan view:clear - Check that you’re on a page that uses the layout with
@routes
Route 'xyz' is not in the route list
Route 'xyz' is not in the route list
Problem:
Ziggy error: route 'posts.show' is not in the route listSolution:- Verify the route exists:
php artisan route:list --name=posts.show - Make sure the route has a name:
->name('posts.show') - Clear route cache:
php artisan route:clear - Reload the page to get the latest route list
Generated URL is wrong
Generated URL is wrong
Problem: URLs have wrong domain or protocol (http vs https)Solution:
- Check your
APP_URLin.env - Configure trusted proxies if behind a load balancer
- See the TLS/SSL termination docs
Next Steps
Route Parameters
Learn advanced parameter handling and route model binding
Filtering Routes
Control which routes are exposed to JavaScript
TypeScript Setup
Enable route name autocompletion with TypeScript
Framework Integration
Deep dives for Vue, React, and other frameworks
You’re All Set!
Congratulations! You’ve successfully:1
Installed Ziggy
✓
2
Added the @routes directive
✓
3
Created named routes
✓
4
Used route() in JavaScript
✓