jinxes/jinxkit

There is no license information available for the latest version (dev-master) of this package.

A restful router

dev-master 2018-03-25 14:13 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:02:47 UTC


README

A minimal restful router

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.

It is suitable as the basis for some customized web frameworks.

Supported PHP versions

I was tested on 5.5 and 7.1 (linux/windows), it work properly

Installation

composer require jinxes/jinxkit dev-master

Get start

  • require the Route
require 'vendor/autoload.php';

use Jinxes\Jinxkit\Route;
  • define a controller and connect to the router
class SayHello
{
    public function say($lang)
    {
        echo 'hello ' . $lang;
    }
}

Route::get('sayhello/:str', SayHello::class, 'say');

Route::start();
  • Open the development Server for test
php -S localhost:8080

and visit: http://localhost:8080/index.php/sayhello/world

hello world
  • embed router defind a embed router
Route::get('embed/:str', function($word) {
    echo $word;
});

visit: http://localhost:8080/index.php/embed/hello

hello
  • add some filters define a filter class and add for the SayHello router
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:

filter pass 
hello world
  • define a RESTful router
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

hello world

visit: http://localhost:8080/index.php/api

this is a GET request

dependency injection for filter and controller

  • Define a Service class
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

  • inject services
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

hello world
  • filter and embed function also support DI container.

update by Jinxes blldxt@yahoo.com