Download the PHP package tightenco/ziggy without Composer
On this page you can find all versions of the php package tightenco/ziggy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tightenco/ziggy
More information about tightenco/ziggy
Files in tightenco/ziggy
Package ziggy
Short Description Use your Laravel named routes in JavaScript.
License MIT
Homepage https://github.com/tighten/ziggy
Informations about the package ziggy
Ziggy – Use your Laravel routes in JavaScript
Ziggy provides a JavaScript route()
function that works like Laravel's, making it a breeze to use your named Laravel routes in JavaScript.
- Installation
- Usage
route()
functionRouter
class- Route-model binding
- TypeScript
- JavaScript frameworks
- Generating and importing Ziggy's configuration
- Importing the
route()
function - Vue
- React
- SPAs or separate repos
- Filtering Routes
- Including/excluding routes
- Filtering with groups
- Other
- Contributing
Installation
Install Ziggy in your Laravel app with Composer:
Add the @routes
Blade directive to your main layout (before your application's JavaScript), and the route()
helper function will be available globally!
By default, the output of the
@routes
Blade directive includes a list of all your application's routes and their parameters. This route list is included in the HTML of the page and can be viewed by end users. To configure which routes are included in this list, or to show and hide different routes on different pages, see Filtering Routes.
Usage
route()
function
Ziggy's route()
function works like Laravel's route()
helper—you can pass it the name of a route, and the parameters you want to pass to the route, and it will generate a URL.
Basic usage
Parameters
Multiple parameters
Query parameters
Ziggy adds arguments that don't match any named route parameters as query parameters:
If you need to pass a query parameter with the same name as a route parameter, nest it under the special _query
key:
Like Laravel, Ziggy automatically encodes boolean query parameters as integers in the query string:
Default parameter values
Ziggy supports default route parameter values (Laravel docs).
Examples
HTTP request with axios
:
Router
class
Calling Ziggy's route()
function with no arguments will return an instance of its JavaScript Router
class, which has some other useful properties and methods.
Check the current route: route().current()
route().current()
optionally accepts parameters as its second argument, and will check that their values also match in the current URL:
Check if a route exists: route().has()
Retrieve the current route params: route().params
Note: parameter values retrieved with
route().params
will always be returned as strings.
Route-model binding
Ziggy supports Laravel's route-model binding, and can even recognize custom route key names. If you pass route()
a JavaScript object as a route parameter, Ziggy will use the registered route-model binding keys for that route to find the correct parameter value inside the object. If no route-model binding keys are explicitly registered for a parameter, Ziggy will use the object's id
key.
Ziggy also supports custom keys for scoped bindings declared directly in a route definition:
TypeScript
Ziggy includes TypeScript type definitions, and an Artisan command that can generate additional type definitions to enable route name and parameter autocompletion.
To generate route types, run the ziggy:generate
command with the --types
or --types-only
option:
To make your IDE aware that Ziggy's route()
helper is available globally, and to type it correctly, add a declaration like this in a .d.ts
file somewhere in your project:
If you don't have Ziggy's NPM package installed, add the following to your jsconfig.json
or tsconfig.json
to load Ziggy's types from your vendor directory:
JavaScript frameworks
[!NOTE] Many applications don't need the additional setup described here—the
@routes
Blade directive makes Ziggy'sroute()
function and config available globally, including within bundled JavaScript files.
If you are not using the @routes
Blade directive, you can import Ziggy's route()
function and configuration directly into JavaScript/TypeScript files.
Generating and importing Ziggy's configuration
Ziggy provides an Artisan command to output its config and routes to a file:
This command places your configuration in resources/js/ziggy.js
by default, but you can customize this path by passing an argument to the Artisan command or setting the ziggy.output.path
config value.
The file ziggy:generate
creates looks something like this:
Importing the route()
function
You can import Ziggy like any other JavaScript library. Without the @routes
Blade directive Ziggy's config is not available globally, so it must be passed to the route()
function manually:
To simplify importing the route()
function, you can create an alias to the vendor path:
Now your imports can be shortened to:
Vue
Ziggy includes a Vue plugin to make it easy to use the route()
helper throughout your Vue app:
Now you can use the route()
function anywhere in your Vue components and templates:
With <script setup>
in Vue 3 you can use inject
to make the route()
function available in your component script:
If you are not using the @routes
Blade directive, import Ziggy's configuration too and pass it to .use()
:
If you're using TypeScript, you may need to add the following declaration to a .d.ts
file in your project to avoid type errors when using the route()
function in your Vue component templates:
React
Ziggy includes a useRoute()
hook to make it easy to use the route()
helper in your React app:
If you are not using the @routes
Blade directive, import Ziggy's configuration too and pass it to useRoute()
:
You can also make the Ziggy
config object available globally, so you can call useRoute()
without passing Ziggy's configuration to it every time:
SPAs or separate repos
Ziggy's route()
function is available as an NPM package, for use in JavaScript projects managed separately from their Laravel backend (i.e. without Composer or a vendor
directory). You can install the NPM package with npm install ziggy-js
.
To make your routes available on the frontend for this function to use, you can either run php artisan ziggy:generate
and add the generated config file to your frontend project, or you can return Ziggy's config as JSON from an endpoint in your Laravel API (see Retrieving Ziggy's config from an API endpoint below for an example of how to set this up).
Filtering Routes
Ziggy supports filtering the list of routes it outputs, which is useful if you have certain routes that you don't want to be included and visible in your HTML source.
[!IMPORTANT] Hiding routes from Ziggy's output is not a replacement for thorough authentication and authorization. Routes that should not be accessibly publicly should be protected by authentication whether they're filtered out of Ziggy's output or not.
Including/excluding routes
To set up route filtering, create a config file in your Laravel app at config/ziggy.php
and add either an only
or except
key containing an array of route name patterns.
Note: You have to choose one or the other. Setting both
only
andexcept
will disable filtering altogether and return all named routes.
You can use asterisks as wildcards in route filters. In the example below, admin.*
will exclude routes named admin.login
, admin.register
, etc.:
Filtering with groups
You can also define groups of routes that you want make available in different places in your app, using a groups
key in your config file:
Then, you can expose a specific group by passing the group name into the @routes
Blade directive:
To expose multiple groups you can pass an array of group names:
Note: Passing group names to the
@routes
directive will always take precedence over your otheronly
orexcept
settings.
Other
TLS/SSL termination and trusted proxies
If your application is using TLS/SSL termination or is behind a load balancer or proxy, or if it's hosted on a service that is, Ziggy may generate URLs with a scheme of http
instead of https
, even if your app URL uses https
. To fix this, set up your Laravel app's trusted proxies according to the documentation on Configuring Trusted Proxies.
Using @routes
with a Content Security Policy
A Content Security Policy (CSP) may block inline scripts, including those output by Ziggy's @routes
Blade directive. If you have a CSP and are using a nonce to flag safe inline scripts, you can pass the nonce to the @routes
directive and it will be added to Ziggy's script tag:
Disabling the route()
helper
If you only want to use the @routes
directive to make Ziggy's configuration available in JavaScript, but don't need the route()
helper function, set the ziggy.skip-route-function
config to true
.
Retrieving Ziggy's config from an API endpoint
If you need to retrieve Ziggy's config from your Laravel backend over the network, you can create a route that looks something like this:
Re-generating the routes file when your app routes change
If you are generating your Ziggy config as a file by running php artisan ziggy:generate
, you may want to re-run that command when your app's route files change. The example below is a Laravel Mix plugin, but similar functionality could be achieved without Mix. Huge thanks to Nuno Rodrigues for the idea and a sample implementation. See #655 for a Vite example.
Laravel Mix plugin example
Contributing
To get started contributing to Ziggy, check out the contribution guide.
Credits
Thanks to Caleb Porzio, Adam Wathan, and Jeffrey Way for help solidifying the idea.
Security
Please review our security policy on how to report security vulnerabilities.
License
Ziggy is open-source software released under the MIT license. See LICENSE for more information.