Skip to main content

Quickstart Guide

This guide will walk you through creating your first Ziggy-powered feature from a fresh Laravel installation to making your first route() 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 to routes/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 (typically resources/views/layouts/app.blade.php or create it if it doesn’t exist):
resources/views/layouts/app.blade.php
Place @routes before @vite or any other JavaScript to ensure routes are available when your scripts load.

Step 4: Create a View with JavaScript

Create a posts listing page at resources/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 in resources/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
Then use it in your components:
PostsList.vue

With React

PostsList.jsx

With Form Submissions

Debugging Tips

Problem: Uncaught ReferenceError: route is not definedSolution:
  • Ensure @routes is 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
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
Problem: URLs have wrong domain or protocol (http vs https)Solution:

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

You’re now ready to build amazing features with type-safe routing!