Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

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
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech