PHPackages                             lexxyungcarter/ziggy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. lexxyungcarter/ziggy

ActiveLibrary[API Development](/categories/api)

lexxyungcarter/ziggy
====================

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

1.0.2(6y ago)21.7kMITJavaScript

Since Aug 4Pushed 6y ago1 watchersCompare

[ Source](https://github.com/lexxyungcarter/ziggy)[ Packagist](https://packagist.org/packages/lexxyungcarter/ziggy)[ RSS](/packages/lexxyungcarter-ziggy/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (3)Versions (33)Used By (0)

[![Ziggy - Use your Laravel Named Routes inside JavaScript](https://raw.githubusercontent.com/tightenco/ziggy/master/ziggy-banner.png?version=2)](https://raw.githubusercontent.com/tightenco/ziggy/master/ziggy-banner.png?version=2)

Ziggy - Use your Laravel Named Routes inside JavaScript
=======================================================

[](#ziggy---use-your-laravel-named-routes-inside-javascript)

[![TravisCi Status for tightenco/ziggy](https://camo.githubusercontent.com/0b1b1474d01385a1cf675304f4cdcdd6fbe7d5e9f94ed8b38728f3c8328a87e1/68747470733a2f2f7472617669732d63692e6f72672f7469676874656e636f2f7a696767792e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/0b1b1474d01385a1cf675304f4cdcdd6fbe7d5e9f94ed8b38728f3c8328a87e1/68747470733a2f2f7472617669732d63692e6f72672f7469676874656e636f2f7a696767792e7376673f6272616e63683d6d6173746572)[![Total Downloads](https://camo.githubusercontent.com/498603b3cfc25354efb1517ff80c8edb75fdcf0be9d618bb11270d3488390f25/68747470733a2f2f706f7365722e707567782e6f72672f6c65787879756e676361727465722f7a696767792f646f776e6c6f616473)](https://camo.githubusercontent.com/498603b3cfc25354efb1517ff80c8edb75fdcf0be9d618bb11270d3488390f25/68747470733a2f2f706f7365722e707567782e6f72672f6c65787879756e676361727465722f7a696767792f646f776e6c6f616473)[![Latest Stable Version](https://camo.githubusercontent.com/c2fe543817864a4d421240ee4c50eb900e5474e5c2c11ca608b5563a129974e5/68747470733a2f2f706f7365722e707567782e6f72672f6c65787879756e676361727465722f7a696767792f762f737461626c65)](https://camo.githubusercontent.com/c2fe543817864a4d421240ee4c50eb900e5474e5c2c11ca608b5563a129974e5/68747470733a2f2f706f7365722e707567782e6f72672f6c65787879756e676361727465722f7a696767792f762f737461626c65)[![License](https://camo.githubusercontent.com/786f376198d7235b9855d8c265119514631de7db0061432db3f88d0616e8ed16/68747470733a2f2f706f7365722e707567782e6f72672f6c65787879756e676361727465722f636861746d657373656e6765722f6c6963656e7365)](https://camo.githubusercontent.com/786f376198d7235b9855d8c265119514631de7db0061432db3f88d0616e8ed16/68747470733a2f2f706f7365722e707567782e6f72672f6c65787879756e676361727465722f636861746d657373656e6765722f6c6963656e7365)

Ziggy creates a Blade directive which you can include in your views. This will export a JavaScript object of your application's named routes, keyed by their names (aliases), as well as a global `route()` helper function which you can use to access your routes in your JavaScript.

Installation
------------

[](#installation)

1. Add Ziggy to your Composer file: `composer require lexxyungcarter/ziggy`
2. (if Laravel 5.4) Add `Lexx\Ziggy\ZiggyServiceProvider::class` to the `providers` array in your `config/app.php`.
3. Include our Blade Directive (`@routes`) somewhere in your template before your main application JavaScript is loaded—likely in the header somewhere.

Usage
-----

[](#usage)

This package replaces the `@routes` directive with a collection of all of your application's routes, keyed by their names. This collection is available at `Ziggy.namedRoutes`.

The package also creates an optional `route()` JavaScript helper which functions like Laravel's `route()` PHP helper, which can be used to retrieve URLs by name and (optionally) parameters.

### Examples:

[](#examples)

Without parameters:

```
route('posts.index') // Returns '/posts'
```

With required parameter:

```
route('posts.show', {id: 1}) // Returns '/posts/1'
route('posts.show', [1]) // Returns '/posts/1'
route('posts.show', 1) // Returns '/posts/1'
```

With multiple required parameters:

```
route('events.venues.show', {event: 1, venue: 2}) // Returns '/events/1/venues/2'
route('events.venues.show', [1, 2]) // Returns '/events/1/venues/2'
```

With query parameters:

```
route('events.venues.show', {event: 1, venue: 2, page: 5, count: 10}) // Returns '/events/1/venues/2?page=5&count=10'
```

If whole objects are passed, Ziggy will automatically look for `id` primary key:

```
var event = {id: 1, name: 'World Series'};
var venue = {id: 2, name: 'Rogers Centre'};

route('events.venues.show', [event, venue]) // Returns '/events/1/venues/2'
```

Practical AJAX example:

```
var post = {id: 1, title: 'Ziggy Stardust'};

return axios.get(route('posts.show', post))
    .then((response) => {
        return response.data;
    });
```

### Default Values

[](#default-values)

See Laravel [documentation](https://laravel.com/docs/5.5/urls#default-values)

Default values work out of the box for Laravel versions &gt;= 5.5.29, for the previous versions you will need to set the default parameters by including this code somewhere in the same page as our Blade Directive (@routes)

```
Ziggy.defaultParameters = {
    //example
    locale: "en"
}
```

Filtering Routes
----------------

[](#filtering-routes)

Filtering routes is *completely* optional. If you want to pass all of your routes to JavaScript by default, you can carry on using Ziggy as described above.

### Basic Whitelisting &amp; Blacklisting

[](#basic-whitelisting--blacklisting)

To take advantage of basic whitelisting or blacklisting of routes, you will first need to create a standard config file called `ziggy.php` in the `config/` directory of your Laravel app and set **either** the `whitelist` or `blacklist` setting to an array of route names.

**Note: You've got to choose one or the other. Setting `whitelist` and `blacklist` will disable filtering altogether and simply return the default list of routes.**

#### Example `config/ziggy.php`:

[](#example-configziggyphp)

```
