Ziggy

tightenco/ziggy image

Ziggy stats

Downloads
12.4M
Stars
3,419
Open Issues
4
Forks
253

View on GitHub →

Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.

Ziggy – Use Your Laravel Routes in JavaScript

Ziggy is a Laravel package designed to make Laravel's named routes accessible in JavaScript, providing a seamless way to generate URLs based on your routes directly from your client-side scripts. It's compatible with Laravel version 5.4 and up, and supports all modern browsers.

Key Features:

  • JavaScript Route Helper: Mimics Laravel's PHP route() helper, allowing you to generate URLs for your named routes in JavaScript.
  • Support for Route Parameters: Pass parameters to your routes just like you do in Laravel, including support for route-model binding.
  • Integration with JavaScript Frameworks: Works out-of-the-box with popular frameworks like Vue and React, including a Vue plugin and a React hook.
  • TypeScript Support: Comes with unofficial TypeScript definitions to help integrate with TypeScript projects.
  • Advanced Route Filtering: Provides options to filter which routes are made available to your JavaScript, optimizing load times and enhancing security.
  • Flexible Setup Options: Can be used with the Blade directive or via an API endpoint, making it adaptable to various application architectures including SPAs.

Installation:

  1. Add Ziggy to your Laravel project:
    composer require tightenco/ziggy
  2. Include the @routes Blade directive in your main layout file, before any JavaScript is loaded.

Usage:

  • Generating URLs:
    route('posts.show', { id: 1 }); // Generates URL for the posts.show route with id 1
  • Accessing Route Functions: When called with no arguments, route() returns a Router instance, allowing access to methods like current(), has(), and properties like params.

Advanced Setup:

  • Non-Blade Environments: Use php artisan ziggy:generate to create a routes file, or configure an endpoint to provide route configuration as JSON.
  • Framework Integration:
    • Vue: Use the ZiggyVue plugin for easy integration.
    • React: Utilize the useRoute() hook for seamless route generation.

Practical Considerations:

  • Security with Content Security Policies (CSP): If using CSP, pass a nonce to the @routes directive to enable inline script execution.
  • TLS/SSL Termination: Ensure proper proxy configuration to prevent URL scheme mismatches in generated URLs.

Contributing:

Contributors are welcome! Check the GitHub repository for issues that need help.

License:

Ziggy is open-source software licensed under the MIT license.

For comprehensive details on setup and usage, refer to the package's full documentation and GitHub repository.

tighten photo

We're not here anymore! We're at github.com/tighten

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Tightenco Ziggy Related Articles

Ziggy: A Package for Named Laravel Routes in JavaScript image

Ziggy: A Package for Named Laravel Routes in JavaScript

Read article
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises