jet/router

Simple package to build RESTful web application

dev-master 2017-09-26 14:18 UTC

This package is not auto-updated.

Last update: 2024-04-27 11:21:04 UTC


README

Travis status

Jet Router

The Jet Router package help you to create RESTfull application with named parametters and real testable url

Installation

Just download the git repository and get and autoload. Or load file manually. But I realy recommand to get an autoload file (realy, it's just crazy to load all file manualy).

Jet/Router will be release to composer soon.

How to use

Before using this router, you need to create a .htaccess file to redirect all uri to your frontController or whatever for routing (replace index.php with your file) :

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

To use this router package, you just need to create your urls :

<?php

namespace Your\Namespace;

use Jet\Router\Router;

$router = new Router(array(
    '/' => function(){
        //Do something here on the default URL
    },
    'error' => function($error){
        //Do something when url don't exists
    },
    'article/[page]:num' => '\Your\Namespace\Class:method',
));

//Or add url via addRoutes

$router->addRoutes(array(
    'article/:slug/[id]:num' => function($id) {
        //Do something with article ID
    }
));

//And now, launch your router !

$router->launch();

You can also add multiple function or class to one route :

<?php

namespace Your\Namespace;

use Jet\Router\Router;

$router = new Router(array(
    '/' => array(
        function(){
            //Do something here on the default URL
        },
        function(){
            //Do something else
        },
        '\Your\Namespace\Class:method',
        '\Your\Other\Namespace\Class:method'        
    )
));

Use patterns in routes

  • :any = allow any char in url
  • :slug = allow slugify text (this-is-a-tile-2493/dfds_fds)
  • :alpha = allow alpha char
  • :num = allow numbers

Any pattern can be named and used with the same name in the asked function/method : [name]:pattern

Usefull tips

Jet/Router provide some tools to detect XHR request and method type :

  • Router::isXHR() : detect is the uri is called via an XHR request
  • Router::getMethod(): the method used for the asked url

Test

Just run phpunit or see the travis status

Feel free to contribute!

  • Fork
  • Report bug
  • Help in development
  • Buy me a new mac (what ?)

Licence

Released under a BSD license