Slim Router JS
Generate Slim Router path URLs using Javascript.
This package provides Javascript code to generate URLs for
named routes
in Slim Framework using Javascript:, (*1)
var url = Slim.Router.pathFor('hello', {'name': 'World'});
The Slim.Router
object provides the methods pathFor()
and relativePathFor()
which work the same as the
Slim\Router::pathFor()
and Slim\Router:relativePathFor()
methods in PHP., (*2)
Installation
Install the package using composer:, (*3)
composer require llvdl/slim-router-js
Then add a route to generate the Javascript code for the Slim.Router
object:, (*4)
$app = new \Slim\App();
// Add router javascript
$app->get('/router.js', function($req, $res, $args) {
$routerJs = new \Llvdl\Slim\RouterJs($this->router);
return $routerJs->getRouterJavascriptResponse();
});
Note: router.js
is considered as a static file by the PHP built-in
webserver. Either use a router script, or use a pattern without an extension,
for example '/router`. See the
PHP documentation
for more information., (*5)
Finally, in the HTML file, import the router.js
file:, (*6)
<html>
<head>
<script src="/router.js"></script>
</head>
</html>
Usage
To make a route available in Slim.Router
in javascript, add a name to it:, (*7)
$app->get('/hello/{name}', function($req, $res) {
// ...
})->setName('hello');
Note: routes without a name are not available to Slim.Router
in javascript., (*8)
In the HTML document, import router.js
. The URL for the named route can then be generated using Slim.Router.pathFor
:, (*9)
<html>
<head>
<script src="/router.js"></script>
</head>
<body>
<input id="name" type="text"/>
<button id="submit-button">Go</button>
<script>
document.getElementById('submit-button').on('click', function() {
var name = document.getElementById('name').value;
var url = Slim.Router.pathFor('hello', {name: name});
alert(url);
});
</script>
</body>
</html>
See the example/
folder in this repository for an example script., (*10)
RouterJs methods:
RouterJs is the PHP class that generates the Javascript code. It provides
the following methods:, (*11)
-
__constructor(\Slim\Router $router, bool $minified)
: constructor, (*12)
By default a minified javascript is returned. Set $minified
to false for
non-minified javascript code., (*13)
-
getRouterJavascriptResponse(): Slim\Response
, (*14)
Returns a HTTP response for use in an action, (*15)
-
getRouterJavascript(): string
, (*16)
Generates the javascript code, (*17)
Slim.Router methods
The Slim.Router
object provides the following methods:, (*18)
pathFor(name, data, queryParams)
relativePathFor(name, data, queryParams)
These method work as the Slim\Router::pathFor()
and
Slim\Router::relativePathFor()
methods in PHP., (*19)
Tests
The repository contains PHPUnit tests for the PHP code. To run these:, (*20)
phpunit -c app/phpunit.xml
There are no automatic tests for the javascript code. It has been
manually tested, and found to be working, using the
example code in the following browsers:, (*21)
- Chrome and Chromium
- Firefox
- Microsoft Edge
- Microsoft Internet Explorer 11
Todo
This section lists some future functional improvements that can be made:, (*22)
-
Filter exposed routes, for example by route argument, (*23)
-
Caching, (*24)
Note that middleware can be used to cache the response., (*25)
-
Allow for inclusing in a Javascript bundler, for example Webpack, (*26)