
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
laravel-js-routes
Advanced tools
A simple front to back router package for JavaScript. Useful for users of dynamic JavaScript frameworks like Vue.js or React who are using ajax calls. The routes can be added manually or generated automatically for a given resource (e.g. photos).
Features and benefits:
Using npm.
npm install laravel-js-routes
Import the package in JavaScript.
// Using commonjs
var LaravelRoutes = require('laravel-js-routes');
// Using es2015
import LaravelRoutes from 'laravel-js-routes';
Create a new routes object for the application.
var r = new LaravelRoutes();
Access a "create" resource route for the group photos.
var route = r.route('photos.create');
// or
var route = r.group('photos').route('create');
Return a formatted table string of all the routes.
var routeListString = r.list();
// For debugging
console.log(routeListString);
Route methods are used to access the object data.
/**
* The link with parameters bound.
*/
route.url();
route.url(parameters);
/**
* The link with parameter placeholders.
*/
route.uri();
/**
* UrlParams object used by the route.
*/
route.urlParameters();
/**
* The preferred http method verb, for example GET.
* Proxy 'method()' added in (v2.0.0+)
*/
route.verb();
route.method();
/**
* An array of all supported verbs.
*/
route.verbs();
/**
* The second half of the name if route is in a group. Same as route.name() if called on a headless route.
*/
route.action();
/**
* The full qualified name of the route.
*/
route.name();
The UrlParams object returned by route.urlParameters() contains a couple of useful methods.
/**
* An array of all the required parameter names.
*/
params.required();
/**
* Check wether the route requires any parameters or not.
*/
params.areRequired();
/**
* Directly check if a given parameters object contains the keys required by the route.
*/
params.objectHasRequiredKeys(object);
Parameters are bound when calling the route.url() method. Obviously, if the route has no url parameters, you don't have to give .url() any parameters. The method accepts one of the following types as a parameter:
When using a single parameter, the first method is often desirable.
var route = r.add('GET', 'example/{parameter}', 'test');
var url = route.url(1);
// url = example/1
When multiple parameters are required, arrays can be used to bind the values in the same order as in the given array.
var route = r.add('GET', 'example/{p1}/{p2}/{p3}', 'test');
var url = route.url([1,2,3]);
// url = example/1/2/3
For most fine grained control and making sure that every parameter matches as intended by the user, using a keyed object is the method of choice.
var route = r.add('GET', 'example/{fruit}/{vegetable}/{meat}', 'test');
var url = route.url({
meat: 'beef',
fruit: 'orange',
vegetable: 'cucumber'
});
// url = example/orange/cucumber/beef
You can now bind regular get parameters to the url string when using object type binding (see section above). The binding is applied automatically to GET routes.
var route = r.add('GET', 'example');
var url = route.url({
foo: 1,
bar: 2
});
// url = example?foo=1&bar=2
Note that when binding parameters, the route parameters of a given route are given priority over GET parameters.
var route = r.add('GET', 'example/{foo}');
var url = route.url({
foo: 1,
bar: 2
});
// url = example/1?bar=2
Setting name | Type | Default | Status | Explanation |
---|---|---|---|---|
registration.strict | boolean | false | 2.1.0 | Throw errors when using Laravel style registration and name of the new route is not explicitly defined. |
registration.explicit | boolean | false | n/a | Disable automatic resource routes. |
logging.warnings | boolean | true | 2.1.0 | Show warnings in console when using Laravel style registration and name of the new route is not explicitly defined. |
logging.suggestions | boolean | true | n/a | Show tips and debugging suggestions in longer error messages. |
The $ sign can be used as a placeholder in the route uri when adding routes to a group. Url parameters can be created using the same curly bracket syntax as in laravel. The text inside brackets will be used as a parameter name so avoid using duplicate parameter names if you want the binding to work correctly with objects.
Adding multiple routes.
r.group('photos').addAll({
upload: ['POST', '$/upload'],
publish: ['PUT/PATCH', '$/{photo}/publish']
});
// or
r.addAll({
"photos.upload" : ['POST', '$/upload'],
"photos.publish" : ['PUT/PATCH', '$/{photo}/publish']
});
Adding a single route.
r.add(verb, uri, action, groupName);
// or
r.group('photos').add(verb, uri, action);
Verb | URI | Action | Route Name |
---|---|---|---|
POST | /photos/upload | upload | photos.upload |
PUT/PATCH | /photos/{photo}/publish | publish | photos.publish |
FAQs
Js helpers for creating laravel resource routes.
We found that laravel-js-routes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.