The Jet Router package help you to create RESTfull application with named parametters and real testable url
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.
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'
)
));
: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
Jet/Router provide some tools to detect XHR request and method type :
Router::isXHR()
: detect is the uri is called via an XHR requestRouter::getMethod()
: the method used for the asked url
Just run phpunit
or see the travis status
- Fork
- Report bug
- Help in development
- Buy me a new mac (what ?)
Released under a BSD license