2017 © Pedro Peláez
 

library jinxkit

A restful router

image

jinxes/jinxkit

A restful router

  • Sunday, March 25, 2018
  • by while
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Jinxes/Jinxkit

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)

Supported PHP versions

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

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, (*5)

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

visit: http://localhost:8080/index.php/embed/hello, (*6)

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:, (*7)

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, (*8)

hello world

visit: http://localhost:8080/index.php/api, (*9)

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, (*10)

  • 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, (*11)

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

update by Jinxes blldxt@yahoo.com

The Versions

25/03 2018

dev-master

9999999-dev

A restful router

  Sources   Download

The Requires

  • php >=5.5.0

 

by Avatar Jinxes

25/03 2018

dev-lab

dev-lab

A restful router

  Sources   Download

The Requires

  • php >=5.5.0

 

by Avatar Jinxes