Ziggy 1.0
includes significant improvements and changes, most of which won't require any changes to existing code!
TL;DR – If all you're doing is dropping the @routes
Blade directive into a view somewhere and using the Javascript route()
helper function later, you only really need to worry about one thing:
route()
with any arguments returns a string now, so:- Anywhere you're calling
.url()
to get a literal string, remove it. - Anywhere you're passing route paramaters using
.with()
, pass them as the second argument toroute()
instead. - Anywhere you're passing query paramaters using
.withQuery()
, pass them along with your route parameters in the second argument toroute()
. (If any of their names collide with your route parameters, nest your query parameters under a_query
key.)
- Anywhere you're calling
- New features
- High-impact changes
- Medium-impact changes
- Low-impact changes
with()
andwithQuery()
methods removedRoute
Facade macros removedRoutePayload
renamed toZiggy
getRoutePayload()
method removedUrlBuilder
class removedbaseProtocol
andbaseDomain
properties removedbase
and other prefixes removedfilter()
method made fluent- Unused PHP methods removed
- Internal PHP methods made private
- Undocumented Javascript methods removed
- Javascript build asset renamed to
index.js
check()
method deprecated
-
Ziggy now fully supports Laravel's route-model binding functionality.
Previously, Ziggy could accept an object as a parameter and use its
id
key as the actual parameter value in the URL, allowing you to pass, for example, a Javascript object representing an instance of one of your Laravel models, directly into theroute()
function.This feature has been fleshed out to more fully support route-model binding in two key ways:
- Ziggy now fully supports custom scoped route-model binding defined in route definitions, e.g.
/users/{user}/posts/{post:uuid}
. - Ziggy now supports implicit route-model binding defined by type-hinting controller methods and closures.
For example, take the following model and route:
class Post extends Model { public function getRouteKeyName() { return 'slug'; } }
Route::post('posts/{post}', function (Post $post) { return view('posts.show', ['post' => $post]); })->name('posts.update');
In Ziggy v1, you can pass an object with a
slug
key into theroute()
helper, and the slug will be used as the route parameter value:const post = { id: 15, slug: 'announcing-ziggy-v1', author: 'Jacob', published: false }; route('posts.update', post); // 'https://ziggy.test/posts/announcing-ziggy-v1'
- Ziggy now fully supports custom scoped route-model binding defined in route definitions, e.g.
-
Ziggy now supports matching parameters using
current()
.Ziggy's
current()
method, which can be passed a route name to check if the browser is currently 'on' that route, can now be passed an object of parameters as the second argument, and will return whether those parameter values match in the current URL.This addition makes the following checks possible:
// Route called 'events.venues.show', with URI '/events/{event}/venues/{venue}' // Window URL is https://myapp.com/events/1/venues/2?authors=all // Before (unchanged) route().current(); // 'events.venues.show' route().current('events.venues.show'); // true // New in Ziggy v1 route().current('events.venues.show', { event: 1, venue: 2 }); // true route().current('events.venues.show', { authors: 'all' }); // true route().current('events.venues.show', { venue: 6 }); // false
-
The
route()
function now returns a literal string if it is passed any arguments.If you are chaining methods onto a
route(...)
call with arguments, such asroute('posts.show').url()
orroute('home').withQuery(...)
, remove the chained methods. In the case ofroute(...).url()
you can just remove.url()
and nothing will change, for other methods see below.Calls specifically to
route()
, with no arguments, are not affected and will still return an instance of theRouter
class, so things likeroute().current()
androute().params
still work as expected.See #336
-
The
url()
method on theRouter
class was removed and can safely be deleted from projects that used it.Because of the above change to
route(...)
, theurl()
method is no longer necessary. You can safely remove it, e.g. by finding and replacing instances of'.url()'
in your project with''
(nothing).See #336
-
The default
ziggy:generate
path has changed toresources/js/ziggy.js
, Laravel's default javascript asset location.Details
The default output path of the
ziggy:generate
command has changed fromresources/assets/js/ziggy.js
toresources/js/ziggy.js
to bring it in line with the changes to theresources
directory structure introduced in Laravel 5.7.See #269
-
The
whitelist
andblacklist
features were renamed toonly
andexcept
.Details
All
whitelist
andblacklist
functionality, like the config keys and methods, was renamed toonly
andexcept
.See #300
-
Boolean query parameters are now encoded as integers.
Details
Ziggy's
route()
function will now encode boolean query parameters as integers (0
/1
) instead of strings ('true'
/'false'
).See #345
-
The
with()
andwithQuery()
methods were removed.Details
The
with()
andwithQuery()
methods on theRouter
class (the object returned by theroute()
function if it is passed no arguments) are deprecated. Instead ofwith()
, pass parameters as the second argument toroute()
. Instead ofwithQuery()
, you can pass query parameters in the same object with regular parameters, as the second argument toroute()
. If you have query parameters and named parameters with the same name, use the new special_query
key. -
The
Route
Facade macros,Route::whitelist()
andRoute::blacklist()
, were removed.Details
The
Route
Facade macros,Route::only()
andRoute::except()
(previouslyRoute::whitelist()
andRoute::blacklist()
) were removed. Instead of using these macros in your route files, set the routes to include/exclude inconfig/ziggy.php
.See #306
-
The
RoutePayload
class was renamed toZiggy
and refactored.Details
The PHP
RoutePayload
class was renamed toZiggy
and its->compile()
method was removed in favor of constructing a new instance and calling->toArray()
or->toJson()
. Also:- The application router instance is now resolved internally instead of being passed into the constructor, so
new Ziggy(...)
now takes only 2 arguments,$group
and$url
- The default value of
$basePort
was changed fromfalse
tonull
See #305
- The application router instance is now resolved internally instead of being passed into the constructor, so
-
The
getRoutePayload()
method was removed.Details
The
getRoutePayload()
method on the PHPBladeRouteGenerator
andCommandRouteGenerator
classes was removed.See #305
-
The
UrlBuilder
class was removed.Details
The Javascript
UrlBuilder
class was removed. Refer to thetemplate()
getter on the newRoute
class if you need to re-implement this functionality yourself.See #330
-
The
baseProtocol
andbaseDomain
properties were removed from Ziggy's global configuration object.Details
The
baseProtocol
andbaseDomain
keys were removed from Ziggy's config. Both these values were inferred from thebaseUrl
property, which is set to your app URL. Refer to thetemplate()
getter on the newRoute
class if you need to re-implement this functionality yourself.See #337
-
base
and other prefixes were removed from config keys.Details
The
namedRoutes
,defaultParameters
,baseUrl
, andbasePort
configuration properties were renamed toroutes
,defaults
,url
, andport
.See #338
-
The
filter()
method on theZiggy
class is now 'fluent' and returns an instance ofZiggy
.Details
The
filter()
method on theZiggy
class now returns an instance ofZiggy
instead of a collection of routes.See #341
-
Unused PHP methods were removed.
Details
The unused
appendRouteToList()
andisListedAs()
methods, and the redundant/unnecessaryexcept()
andonly()
methods on theZiggy
class, were removed.See #341
-
Some internal methods on Ziggy's PHP classes were made private.
Details
The
nameKeyedRoutes()
,resolveBindings()
,applyFilters()
, andgroup()
methods on theZiggy
class, and thegenerate()
method on theCommandRouteGenerator
class, are now private.See #341
-
Several undocumented methods and properties were removed from the Javascript
Router
class.Details
Several undocumented methods and properties on the
Router
class (the object returned by theroute()
function when it's called with no arguments) were removed. Replace them with the suggestions below or refer to Ziggy's internals if you need to re-implement the functionality yourself.Removed properties:
name
: use the name you were passing intoroute()
as the first argument.absolute
: use the value you were passing intoroute()
as the third argument.ziggy
: use the globalZiggy
configuraton object.urlBuilder
: refer to thetemplate()
getter on the newRoute
class.template
: refer to thetemplate()
getter on the newRoute
class.urlParams
: use the value you were passing intoroute()
as the second argument.queryParams
: use the value you were passing intowithQuery()
, or intoroute()
as the second argument.hydrated
: use the returned URL string.
Removed methods:
normalizeParams()
: refer to the internal_parse()
method.hydrateUrl()
: use the returned URL string.matchUrl()
: usecurrent()
or refer to thecurrent()
method on the newRoute
class.constructQuery()
: use the returned URL string.extractParams()
: refer to the_dehydrate()
method on theRouter
class.parse()
: use the returned URL string.trimParam()
: use.replace(/{|\??}/g, '')
.
See #330
-
Ziggy's main build asset/entrypoint is now called
index.js
instead ofroute.js
.Details
Ziggy's main Javascript source and dist files are now called
index.js
instead ofroute.js
.See #344
-
The
check()
method is deprecated.Details
The
route().check()
method is deprecated and will be removed in a future major version of Ziggy. Useroute().has()
instead.See #330