- How do I install Ziggy in a Laravel project?
- Run `composer require tightenco/ziggy` to install via Composer. Add the `@routes` Blade directive to your main layout file (e.g., `resources/views/layouts/app.blade.php`) before your JavaScript. This injects the `route()` helper globally for all frontend code.
- Does Ziggy work with Laravel 10? Are there breaking changes?
- Yes, Ziggy is fully compatible with Laravel 10+. The package is actively maintained and tested against the latest Laravel versions. No breaking changes have been introduced for Laravel 10, but always check the [release notes](https://github.com/tighten/ziggy/releases) for minor updates.
- Can I use Ziggy with Vue 3 or React 18 in a SPA?
- Absolutely. Ziggy works seamlessly with modern SPAs. For Vue 3, use the `ZiggyVue` plugin or inject the `route()` function in `<script setup>`. For React 18, use the `useRoute` hook. For SPAs, generate routes at build time with `ziggy:generate` to avoid bloating HTML.
- How do I filter routes to reduce payload size?
- Use the `ziggy:routes` configuration in `config/ziggy.php` to include or exclude routes by name or group. For example, exclude admin routes with `'exclude' => ['admin.*']`. This prevents unnecessary routes from being embedded in your HTML or JavaScript bundle.
- Will Ziggy work with custom route-model binding keys (e.g., slugs instead of IDs)?
- Yes, Ziggy supports custom route-model binding keys out of the box. Simply use the same syntax as Laravel’s `route()` helper in PHP. For example, `route('posts.show', { slug: 'my-post' })` will work if your route is defined as `Route::get('/posts/{slug}', ...)->name('posts.show')`.
- How do I generate TypeScript types for Ziggy routes?
- Run `php artisan ziggy:generate --types` to generate TypeScript types for your route names and parameters. This creates a `ziggy.d.ts` file in your `resources/js` directory, providing autocompletion and compile-time safety for route names and parameters.
- Can Ziggy be used in a monorepo with separate frontend/backend repos?
- Yes, Ziggy supports separate repos. Use `ziggy:generate` to export routes to a JSON file (e.g., `routes.json`) in your frontend repo. This file can be imported directly into your JavaScript codebase, ensuring routes stay in sync without coupling repos.
- What’s the best way to test Ziggy-generated URLs in JavaScript?
- Test Ziggy URLs by comparing them against Laravel’s `url()` helper in PHP. Write unit tests in JavaScript (e.g., with Jest) to verify `route()` outputs match expected paths. For example, assert `route('posts.index')` equals `/posts`. Also test dynamic routes with model binding.
- Does Ziggy support Vite or Webpack for optimized builds?
- Yes, Ziggy integrates with Vite and Webpack. For Vite, use the `ziggy-js` package (`npm install ziggy-js`) and configure it in your `vite.config.js`. For Webpack, ensure the `@routes` Blade directive is placed before your JavaScript bundle. Both setups allow Ziggy to work with path aliases.
- What are the alternatives to Ziggy for Laravel route handling in JavaScript?
- Alternatives include manually hardcoding routes (not recommended), using Laravel’s `window.Laravel` global object (limited to basic routes), or custom solutions like `axios` interceptors for URL generation. Ziggy stands out by mirroring Laravel’s `route()` helper exactly, supporting model binding, and offering TypeScript integration.