PHPackages                             llvdl/slim-router-js - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. llvdl/slim-router-js

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

llvdl/slim-router-js
====================

Slim Router for Javascript

1.1.1(8y ago)49.6k[1 issues](https://github.com/llvdl/slim-router-js/issues)MITPHP

Since Dec 19Pushed 8y ago3 watchersCompare

[ Source](https://github.com/llvdl/slim-router-js)[ Packagist](https://packagist.org/packages/llvdl/slim-router-js)[ RSS](/packages/llvdl-slim-router-js/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Slim Router JS
==============

[](#slim-router-js)

```
Generate Slim Router path URLs using Javascript.

```

This package provides Javascript code to generate URLs for [named routes](https://www.slimframework.com/docs/objects/router.html#named-routes)in [Slim Framework](https://www.slimframework.com/) using Javascript:

```
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.

Installation
------------

[](#installation)

Install the package using composer:

```
composer require llvdl/slim-router-js

```

Then add a route to generate the Javascript code for the `Slim.Router` object:

```
$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](http://php.net/manual/en/features.commandline.webserver.php)for more information.

Finally, in the HTML file, import the `router.js` file:

```

```

Usage
-----

[](#usage)

To make a route available in `Slim.Router` in javascript, add a name to it:

```
$app->get('/hello/{name}', function($req, $res) {
  // ...
})->setName('hello');
```

*Note:* routes without a name are not available to `Slim.Router` in javascript.

In the HTML document, import `router.js`. The URL for the named route can then be generated using `Slim.Router.pathFor`:

```

    Go

      document.getElementById('submit-button').on('click', function() {
        var name = document.getElementById('name').value;
        var url = Slim.Router.pathFor('hello', {name: name});
        alert(url);
      });

```

See the [`example/`](./example) folder in this repository for an example script.

RouterJs methods:
-----------------

[](#routerjs-methods)

RouterJs is the PHP class that generates the Javascript code. It provides the following methods:

- `__constructor(\Slim\Router $router, bool $minified)`: constructor

    By default a minified javascript is returned. Set `$minified` to false for non-minified javascript code.
- `getRouterJavascriptResponse(): Slim\Response`

    Returns a HTTP response for use in an action
- `getRouterJavascript(): string`

    Generates the javascript code

Slim.Router methods
-------------------

[](#slimrouter-methods)

The `Slim.Router` object provides the following methods:

- `pathFor(name, data, queryParams)`
- `relativePathFor(name, data, queryParams)`

These method work as the `Slim\Router::pathFor()` and `Slim\Router::relativePathFor()` methods in PHP.

Tests
-----

[](#tests)

The repository contains PHPUnit tests for the PHP code. To run these:

```
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](./example) in the following browsers:

- Chrome and Chromium
- Firefox
- Microsoft Edge
- Microsoft Internet Explorer 11

Todo
----

[](#todo)

This section lists some future functional improvements that can be made:

- Filter exposed routes, for example by route argument
- Caching

    Note that middleware can be used to cache the response.
- Allow for inclusing in a Javascript bundler, for example Webpack

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

2

Last Release

3064d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d1227c70272353d0086d5d5b4d97f8ba260ca0e9fb1976c8e290cf52e653b731?d=identicon)[llvdl](/maintainers/llvdl)

---

Top Contributors

[![llvdl](https://avatars.githubusercontent.com/u/6775525?v=4)](https://github.com/llvdl "llvdl (7 commits)")

---

Tags

javascriptrouterslim-framework

### Embed Badge

![Health badge](/badges/llvdl-slim-router-js/health.svg)

```
[![Health](https://phpackages.com/badges/llvdl-slim-router-js/health.svg)](https://phpackages.com/packages/llvdl-slim-router-js)
```

###  Alternatives

[tightenco/ziggy

Use your Laravel named routes in JavaScript.

4.3k41.6M267](/packages/tightenco-ziggy)[matthiasmullie/minify

CSS &amp; JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.

2.0k30.5M336](/packages/matthiasmullie-minify)[tedivm/jshrink

Javascript Minifier built in PHP

76037.7M139](/packages/tedivm-jshrink)[dkcwd/dkcwd-zf2-munee

Zend Framework 2 module leveraging 'munee' an asset optimisation library developed by Cody Lundquist. You can find munee at http://github.com/meenie/munee

102.1k](/packages/dkcwd-dkcwd-zf2-munee)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
