Wallogit.com
2017 © Pedro Peláez
A restful router
A minimal restful router, (*1)
This is a mini php route library. At the core of Jinxkit is a RESTful and resource-oriented microkernel. It also integrates a simple filter-controller system and a DI (dependency injection) container., (*2)
It is suitable as the basis for some customized web frameworks., (*3)
I was tested on 5.5 and 7.1 (linux/windows), it work properly, (*4)
composer require jinxes/jinxkit dev-master
require 'vendor/autoload.php'; use Jinxes\Jinxkit\Route;
class SayHello
{
public function say($lang)
{
echo 'hello ' . $lang;
}
}
Route::get('sayhello/:str', SayHello::class, 'say');
Route::start();
php -S localhost:8080
and visit: http://localhost:8080/index.php/sayhello/world, (*5)
hello world
Route::get('embed/:str', function($word) {
echo $word;
});
visit: http://localhost:8080/index.php/embed/hello, (*6)
hello
class Filter
{
public function __invoke()
{
$lang = array_shift($this->args);
if ($lang === 'world') {
echo 'filter pass <br />';
} else {
return false;
}
}
}
Route::get('sayhello/:str', SayHello::class, 'say')->filter([Filter::class]);
will show:, (*7)
filter pass hello world
class SayHello
{
public function get()
{
echo 'this is a GET request';
}
public function say($lang)
{
echo 'hello ' . $lang;
}
}
Route::restful('api', SayHello::class, function($router) {
$router->get('sayhello/:str', SayHello::class, 'say');
});
Route::start();
visit: http://localhost:8080/index.php/api/sayhello/world, (*8)
hello world
visit: http://localhost:8080/index.php/api, (*9)
this is a GET request
class SayService
{
public function hello()
{
echo 'hello ';
}
}
service class is some singleton objects maintenance by DI system
and the construct of services also can be injected, (*10)
class SayHello
{
public function say(SayService $sayService, $lang)
{
echo $sayService->hello() . $lang;
}
}
You must put the service in front of all the parameters and declaring with service name
visit: http://localhost:8080/index.php/api/sayhello/world, (*11)
hello world