Download the PHP package microweber-deps/ziggy without Composer
On this page you can find all versions of the php package microweber-deps/ziggy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download microweber-deps/ziggy
More information about microweber-deps/ziggy
Files in microweber-deps/ziggy
Package ziggy
Short Description Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function 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()
helper function that works like Laravel's, making it easy to use your Laravel named routes in JavaScript.
Ziggy supports all versions of Laravel from 5.4
onward, and all modern browsers.
- Installation
- Usage
- The
route()
helper - The
Router
class - Route-model binding
- TypeScript support
- The
- Advanced Setup
- JavaScript frameworks
- Vue
- React
- SPAs or separate repos
- Filtering Routes
- Basic Filtering
- Filtering using Groups
- Other
- Contributing
Installation
Install Ziggy into your Laravel app with composer require tightenco/ziggy
.
Add the @routes
Blade directive to your main layout (before your application's JavaScript), and the route()
helper function will now 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
The route()
helper
Ziggy's route()
helper function works like Laravel's—you can pass it the name of one of your routes, and the parameters you want to pass to the route, and it will return a URL.
Basic usage
With parameters
With multiple parameters
With query parameters
If you have a query parameter with the same name as a route parameter, nest it under a _query
key:
Like Laravel's route()
helper, Ziggy automatically encodes boolean query parameters as integers in the query string:
With default parameter values
See the Laravel documentation on default route parameter values.
Practical AJAX example
The Router
class
Calling Ziggy's route()
helper function with no arguments will return an instance of the JavaScript Router
class, which has some other useful properties and methods.
Checking the current route: route().current()
The current()
method optionally accepts parameters as its second argument, and will check that their values also match in the current URL:
Checking if a route exists: route().has()
Retrieving 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 route-model binding, and can even recognize custom route key names. If you pass route()
a JavaScript object as one of the route parameters, Ziggy will use the registered route-model binding keys for that route to find the parameter value in the object and insert it into the URL (falling back to an id
key if there is one and the route-model binding key isn't present).
Ziggy also supports custom keys for scoped bindings in the route definition (requires Laravel 7+):
TypeScript support
Unofficial TypeScript type definitions for Ziggy are maintained by benallfree as part of Definitely Typed, and can be installed with npm install @types/ziggy-js
.
Advanced Setup
JavaScript frameworks
If you are not using Blade, or would prefer not to use the @routes
directive, Ziggy provides an Artisan command to output its config and routes to a file: php artisan ziggy:generate
. By default this command stores your routes at resources/js/ziggy.js
, but you can customize this path by passing a different value as an argument to the Artisan command or setting the ziggy.output.path
config value. Alternatively, you can return Ziggy's config as JSON from an endpoint in your Laravel API (see Retrieving Ziggy's routes and config from an API endpoint below for an example of how to set this up).
The file generated by php artisan ziggy:generate
will look something like this:
You can optionally create an alias to make importing Ziggy's core source files easier:
Finally, import and use Ziggy like any other JavaScript library. Because the Ziggy config object is not available globally in this setup, you'll usually have to pass it to the route()
function manually:
Vue
Ziggy includes a Vue plugin to make it easy to use the route()
helper throughout your Vue app:
If you use this plugin with the ziggy
import alias shown above, make sure to update the alias to vendor/tightenco/ziggy/dist/vue.es.js
(Vite) or vendor/tightenco/ziggy/dist/vue
(Laravel Mix).
Note: If you use the
@routes
Blade directive in your views, Ziggy's configuration will already be available globally, so you don't need to import theZiggy
config object and pass it intouse()
.
Now you can use route()
anywhere in your Vue components and templates, like so:
React
To use Ziggy with React, start by importing the route()
function and your Ziggy config. Because the Ziggy config object is not available globally in this setup, you'll have to pass it to the route()
function manually:
We're working on adding a Hook to Ziggy to make this cleaner, but for now make sure you pass the configuration object as the fourth argument to the route()
function as shown above.
Note: If you include the
@routes
Blade directive in your views, theroute()
helper will already be available globally, including in your React app, so you don't need to importroute
orZiggy
anywhere.
SPAs or separate repos
Ziggy's route()
helper function is also 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 routes file to your project, or you can return Ziggy's config as JSON from an endpoint in your Laravel API (see Retrieving Ziggy's routes and config from an API endpoint below for an example of how to set this up).
Then, import and use Ziggy as above:
Filtering Routes
Ziggy supports filtering the routes it makes available to your JavaScript, which is great if you have certain routes that you don't want to be included and visible in the source of the response sent back to clients. Filtering routes is optional—by default, Ziggy includes all your application's named routes.
Basic filtering
To set up basic route filtering, create a config file in your Laravel app at config/ziggy.php
and define either an only
or except
setting as 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 also use asterisks as wildcards in route filters. In the example below, admin.*
will exclude routes named admin.login
and admin.register
:
Filtering using 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 avoid this happening, set up your Laravel app's TrustProxies
middleware 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 as as the second argument 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 your app's routes available in JavaScript, but don't need the route()
helper function, set the skip-route-function
config value to true
:
Retrieving Ziggy's routes and config from an API endpoint
Ziggy can easily return its config object as JSON from an endpoint in your Laravel app. For example, you could set up an /api/ziggy
route that looks something like this:
Then, client-side, you could retrieve the config with an HTTP request:
Re-generating the routes file when your app routes change
If you're exporting your Ziggy routes as a file by running php artisan ziggy:generate
, you may want to watch your app's route files and re-run the command automatically whenever they're updated. 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!
Code 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.