16 KiB
Upgrade Guide
Upgrading from 1.x to 2.x
- The PHP package namespace has changed from
Tightenco\ZiggytoTighten\Ziggy(note: the Composer package name,tightenco/ziggy, has not changed). - The
makeDirectorymethod of theCommandRouteGeneratorclass is now private, overriding it is no longer supported. - The deprecated JavaScript
check()method (e.g.route().check('home')) has been removed. Usehas()instead. - Ziggy's JavaScript now provides named exports only, with no default export. Replace
import route from 'ziggy-js'withimport { route } from 'ziggy-js'. - Ziggy's Vue plugin and React hook have moved to the root of the module. Replace imports from
ziggy-js/vueorziggy-js/reactwith imports directly fromziggy-js(e.g.import { route, ZiggyVue } from 'ziggy-js'). - Ziggy now only includes ES Module builds. The default build, which supports all modern browsers, is
./dist/index.js(this is the default when you import fromziggy-jsorvendor/tightenco/ziggy). A legacy ES Module build using fewer new language features is included too, at./dist/index.esm.js. The third build,./dist/route.umd.js, is for internal use in Ziggy's@routesBlade directive. - Ziggy now requires at least Laravel 9 and PHP 8.1.
Upgrading from 0.9.x to 1.x
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_querykey.)
- Anywhere you're calling
Overview
- New features
- High-impact changes
- Medium-impact changes
- Low-impact changes
with()andwithQuery()methods removedRouteFacade macros removedRoutePayloadrenamed toZiggygetRoutePayload()method removedUrlBuilderclass removedbaseProtocolandbaseDomainproperties removedbaseand 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
New features
-
Ziggy now fully supports Laravel's route-model binding functionality.
Previously, Ziggy could accept an object as a parameter and use its
idkey 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
slugkey 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
High impact changes
-
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 theRouterclass, so things likeroute().current()androute().paramsstill work as expected.See #336
-
The
url()method on theRouterclass 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
Medium impact changes
-
The default
ziggy:generatepath has changed toresources/js/ziggy.js, Laravel's default javascript asset location.Details
The default output path of the
ziggy:generatecommand has changed fromresources/assets/js/ziggy.jstoresources/js/ziggy.jsto bring it in line with the changes to theresourcesdirectory structure introduced in Laravel 5.7.See #269
-
The
whitelistandblacklistfeatures were renamed toonlyandexcept.Details
All
whitelistandblacklistfunctionality, like the config keys and methods, was renamed toonlyandexcept.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
Low impact changes
-
The
with()andwithQuery()methods were removed.Details
The
with()andwithQuery()methods on theRouterclass (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_querykey. -
The
RouteFacade macros,Route::whitelist()andRoute::blacklist(), were removed.Details
The
RouteFacade 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
RoutePayloadclass was renamed toZiggyand refactored.Details
The PHP
RoutePayloadclass was renamed toZiggyand 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,$groupand$url - The default value of
$basePortwas changed fromfalsetonull
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 PHPBladeRouteGeneratorandCommandRouteGeneratorclasses was removed.See #305
-
The
UrlBuilderclass was removed.Details
The Javascript
UrlBuilderclass was removed. Refer to thetemplate()getter on the newRouteclass if you need to re-implement this functionality yourself.See #330
-
The
baseProtocolandbaseDomainproperties were removed from Ziggy's global configuration object.Details
The
baseProtocolandbaseDomainkeys were removed from Ziggy's config. Both these values were inferred from thebaseUrlproperty, which is set to your app URL. Refer to thetemplate()getter on the newRouteclass if you need to re-implement this functionality yourself.See #337
-
baseand other prefixes were removed from config keys.Details
The
namedRoutes,defaultParameters,baseUrl, andbasePortconfiguration properties were renamed toroutes,defaults,url, andport.See #338
-
The
filter()method on theZiggyclass is now 'fluent' and returns an instance ofZiggy.Details
The
filter()method on theZiggyclass now returns an instance ofZiggyinstead 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 theZiggyclass, were removed.See #341
-
Some internal methods on Ziggy's PHP classes were made private.
Details
The
nameKeyedRoutes(),resolveBindings(),applyFilters(), andgroup()methods on theZiggyclass, and thegenerate()method on theCommandRouteGeneratorclass, are now private.See #341
-
Several undocumented methods and properties were removed from the Javascript
Routerclass.Details
Several undocumented methods and properties on the
Routerclass (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 globalZiggyconfiguraton object.urlBuilder: refer to thetemplate()getter on the newRouteclass.template: refer to thetemplate()getter on the newRouteclass.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 newRouteclass.constructQuery(): use the returned URL string.extractParams(): refer to the_dehydrate()method on theRouterclass.parse(): use the returned URL string.trimParam(): use.replace(/{|\??}/g, '').
See #330
-
Ziggy's main build asset/entrypoint is now called
index.jsinstead ofroute.js.Details
Ziggy's main Javascript source and dist files are now called
index.jsinstead 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