rafal / javascriptroutingserviceprovider
JavascriptRouting Service Provider provides easy way to access your routes inside Javacript files. It's cool because putting your routes inside js files as static strings sucks.
Requires
- silex/silex: ~1.0
This package is not auto-updated.
Last update: 2025-01-13 16:21:17 UTC
README
JavascriptRouting Service Provider provides easy way to access your routes inside Javacript files. It's cool because putting your routes inside js files as static strings sucks.
Installation (composer)
require: "rafal/javascriptroutingserviceprovider": "1.0.*@dev"
Options
jsrouting.path
- Required. Path whererouter.js
( name by default ) will be created.jsrouting.file_name
- Output file name. Defaultrouter.js
.jsrouting.refresh
- If true routes will be generated on each request. Defaulttrue
.jsrouting.basepath
- If true request basepath will be inserted before each generated route. Defaultfalse
.jsrouting.skip
- Add one (single string) or multiple (array) regular expression to skip routings.
Example
Lets say you have:
$app->register(new Rafal\JavascriptRoutingServiceProvider\JavascriptRoutingServiceProvider(), array(
'jsrouting.path' => __DIR__.'/public/js',
'jsrouting.file_name' => 'router.js',
'jsrouting.refresh' => $app['debug'],
'jsrouting.basepath' => true,
`jsrouting.skip' => array("/^\/_profiler/"),
));
$app->get('/{name}/extensions/are/{what}', function() use($app) {
// your cool code
})->bind('opinion')->value('name', 'Rafals')->assert('what', '(cool|lame)');
Now you have to remeber to include router.js
file in your layout.
From now you can use Router
class inside your JavaScript files. For example:
Router::get('opinion', {name:'Johns', what:'lame'}) # => /project/web/Johns/extensions/are/lame
Router::get('opinion', {what:'cool'}) # => /project/web/Rafals/extensions/are/cool
Router::get('opinion', {what:'cool'}, false) # => /Rafals/extensions/are/cool
Router::get('opinion', {what:'bazinga'}) # ERROR !
As you can se Router class will respect all requirements, and default values defined for your routes. You can overwrite the default request basepath inserting option by the third parameter.
License
JavascriptRouting Service Provider is licensed under the MIT license.