May 29, 2019 3 min read
How I Like to Simplify Ziggy’s Route Helper
Manager, Digital Product Delivery
All I really want from Ziggy is to be able to use a helper in my JS to get a URL, the exact same way as Laravel’s
route helper. For example, if I want the URL for an API endpoint that lists all users, I just want to drop
I want not to do any of the following:
Commit compiled files to git (the route helper returns absolute URLs with your
APP_URLleading the way).
Call any other methods Ziggy’s
Thankfully, configuring things as I prefer is fairly straightforward. All I did was:
update my node scripts to use Ziggy’s Artisan command,
add the output file to my
define my own
routehelper that relies on Ziggy’s.
1. Tweak yer scripts.
package.json looks something like this:
2. Withhold attention.
3. Tell that route helper what’s up.
routes.js exports the routes in an object named
Ziggy, which is what we’re importing on line 2 and passing as the 4th argument to
ZiggyRoute(). The third argument tells Ziggy whether to use absolute URLs.
app.js, I can just drop
import 'global-helpers'; in and call my stripped-down
route helper anywhere.
NOTE: I’ve got this in my
webpack.mix.js (lets me import from
If you run into snags with ESLint or Jest, hit me up and I can share more of the less-directly-relevant setup.