Code Monkey home page Code Monkey logo

slim-router-js's Introduction

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:

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

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 for more information.

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

<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:

$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:

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

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

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

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 in the following browsers:

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

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

slim-router-js's People

Contributors

llvdl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

slim-router-js's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.