This gist adds a route:json'
command to generate a Javascript module RouteHelper.js
capable of resolving urls for the application's routes.
The module encapsulates a json version of the application routes.
It provides methods to resolve URLs with or without arguments, and relative or absolute paths.
The purpose is to provide client-side Javascript route helpers comparable to those available to Laravel code within PHP.
Copy the JsRouteHelper.php class into your app/Console/Commands
folder, and add the classname
to the app/Console/Kernel.php#commands
array:
protected $commands = [
Commands\JsRouteHelper::class,
];
Decide what directory you want the helper script to be dropped into. This gist puts the file into resources/assets/js/api
. To use this as the default you might need to mkdir -p resources/assets/js/api
. Othwise edit the JsRouteHelper.php to put it in a different location. Whereever it is, needs to be available to load either through your HTML headers or a Javascript module packer.
Obviously, you'll need named routes in order for them to be dumped symbolically.
Run php artisan route:json
, import the RouteHelper file, and new up an instance:
import RouteHelper from './api/RouteHelper'
Window.urlTo = new RouteHelper(document.head.querySelector('base').href));
axios.get(urlTo.route('widgets', widgetId)).then((results) => {
// do something with widget
}).catch((failure) {
// do something with failure
});
The helper can be chained as a property off of Axios' object as well. Note: the term route has multiple meanings in the context of a Laravel-backed SPA, and polluting another object with names could lead to shadowed properties.
import RouteHelper from './api/RouteHelper'
let urlTo = new RouteHelper(document.head.querySelector('base').href));
let axios = require('axios').create({ baseURL: urlTo.getBaseUrl(), timeout: 2500 });
axios.urlTo = urlTo;
...
let linkToHome = axios.urlTo.route('home');
...
You can pass positional arguments to the route
helper, or pass only a second argument that is an object with named keys for the parameters. If the profile
route named a "{username}" path segment, the following two forms would be equivalent:
let linkToMyProfile = axios.urlTo.route('profile', { username: this.username })
let linkToMyProfile = axios.urlTo.route('profile', this.username)
By default, the route helper returns a URL rooted in the base url passed to its constructor. If you want to grab a relative path instead, use the relativeTo(path)
method:
let srcUrl = axios.urlTo.relativeTo('https://imgur.com/').route('file-browser')
You can also call urlTo.setBaseUrl(path)
on the routeHelper object, or save the result of calling relativeTo
which is a copy of the routeHelper object.
The method relative()
is shorthand for relativeTo('.')
Both of the methods mentioned here are chainable.
Calling the method getBaseUrl()
on a newly instantiated routeHelper object returns the default root url with which it was constructed. The converse method, setBaseUrl(base)
has the obvious functionality.
The setBaseUrl
method is chainable.
Calling the method setUrlMap(arrtype)
on a newly instantiated routeHelper object overwrites the route mapping as imported from Laravel's routes. Again, the converse method, getUrlMap()
exists.
The setUrlMap
method is chainable.