Download the PHP package amrikasir/ziggy without Composer
On this page you can find all versions of the php package amrikasir/ziggy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download amrikasir/ziggy
More information about amrikasir/ziggy
Files in amrikasir/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
Informations about the package ziggy
just a copy/fork from tightenco/ziggy.
since I need Ziggy with features that I added for my Laravel Project and tightenco response is too long, I'll re-post new Packagist repository
Ziggy - Use your Laravel Named Routes inside JavaScript
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
-
Add Ziggy to your Composer file:
composer require amrikasir/ziggy
orcomposer -vvv require amrikasir/ziggy
if you want to see verbose log -
(if Laravel 5.4) Add
Tightenco\Ziggy\ZiggyServiceProvider::class
to theproviders
array in yourconfig/app.php
. - Include our Blade Directive (
@routes
) somewhere in your template before your main application JavaScript is loaded—likely in the header somewhere.
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:
Without parameters:
With required parameter:
With multiple required parameters:
With query parameters:
If whole objects are passed, Ziggy will automatically look for id
primary key:
If want to use unamed route like
Ziggy privide go
method:
Practical AJAX or Axios example:
Ajax or Axios with unamed route
Default Values
See Laravel documentation
Default values work out of the box for Laravel versions >= 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)
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 & 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
:
As shown in the example above, Ziggy the use of asterisks as wildcards in filters. home
will only match the route named home
whereas api.*
will match any route whose name begins with api.
, such as api.posts.index
and api.users.show
.
Simple Whitelisting & Blacklisting Macros
Whitelisting and blacklisting can also be achieved using the following macros.
Example Whitelisting
Example Blacklisting
Advanced Whitelisting Using Groups
You may also optionally define multiple whitelists by defining groups
in your config/ziggy.php
:
In the above example, you can see we have configured multiple whitelists for different user roles. You may expose a specific whitelist group by passing the group key into @routes
within your blade view. Example:
Note: Using a group will always take precedence over the above mentioned whitelist
and blacklist
settings.
Other useful methods
current()
To get the name of the current route (based on the browser's window.location
) you can use:
To check that we are at a current route, pass the desired route in the only param:
You can even use wildcards:
url()
Ziggy returns a wrapper of the string primitive, which behaves exactly like a string in almost all cases.
In rare cases where third-party libraries use strict type checking, you may require an actual String
literal.
To achieve this simple call .url()
on your route:
Artisan command
Ziggy publishes an artisan command to generate a ziggy.js
routes file, which can be used as part of an asset pipeline such as Laravel Mix.
You can run php artisan ziggy:generate
in your project to generate a static routes file in resources/assets/js/ziggy.js
.
Optionally, include a second parameter to override the path and file names (you must pass a file name with the path):
Example ziggy.js
, where the named routes home
and login
exist in routes/web.php
:
Environment-based loading of minified route helper file
When loading the blade helper file, Ziggy will detect the current environment and minify the output if APP_ENV
is not local
.
When this happens, ziggy.min.js
will be loaded. Otherwise, ziggy.js
will be used.
Optional route
helper
If you only want routes available through @routes
, but don't need the route
helper function, you can include skip-route-function
in your config and set it to true
:
Using with Vue components
If you want to use the route
helper within a Vue component, you'll need to add this to your app.js
file:
Then, use the method in your Vue components like so:
<a class="nav-link" :href="route('home')">Home</a>
Thanks to Archer70 for this solution.
Using with laravel-haml
(and other custom Blade compilers)
laravel-haml provides HAML-based Blade templating. Because laravel-haml uses a separate BladeCompiler
instance, custom directives (like @route
) are not automatically imported. There is a pull request to fix that. In the mean time, simply place this code in your ./app/Providers/AppServiceProvider.php
's boot()
section:
This pattern is not limited to laravel-haml; it can be used to initialize any custom template compilers you may be using.
Contributions & Credits
To get started contributing to Ziggy, just Pull Request your code.
Thanks to Caleb Porzio, Adam Wathan, and Jeffrey Way for help solidifying the idea.