2017 © Pedro Peláez
 

library router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

image

selami/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  • Wednesday, July 11, 2018
  • by mkorkmaz
  • Repository
  • 2 Watchers
  • 0 Stars
  • 277 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 23 % Grown

The README.md

Selami Router

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Codacy Badge, (*1)

Router and Dispatcher built on top of nikic/FastRoute that returns controller class name to be instantiated, desired content-type and route aliases if defined., (*2)

Installation

Install this library using composer:, (*3)

$ composer require selami/router

Example

Get global values., (*4)

<?php
declare(strict_types=1);

$request = new PSR7ServerRequest(); // Let's say this class implements PSR7 ServerRequestInterface
$response = new PSR7ServerResponse(); // Let's say this class implements PSR7 ResponseInterface
$defaultReturnType  = Selami\Router\Router::JSON; // Possible values: Selami\Router\Router::HTML, Selami\Router\Router::JSON, Selami\Router::TEXT, Selami\Router\Router::CUSTOM, Selami\Router\Router::REDIRECT, Selami\Router::DOWNLOAD. To be used to send output.
$requestMethod      = 'GET';        // i.e. $_SERVER['REQUEST_METHOD']
$requestedUri       = '/user/12/inbox'; // i.e. $_SERVER['REQUEST_URI']

Create Selami\Router Instance., (*5)

$router = new Selami\Router(
    $defaultReturnType,
    $requestMethod,
    $requestedUri
);

Add routes that expect HTTP request methods. $route variable uses nikic/FastRoute's route syntax., (*6)

use Selami\Router\Router;

$route = '/';
$action = Controllers\Home::class;
$returnType = Router::HTML;
$alias = 'home';

$router->get(
    $route,         // required
    $action,        // required
    $returnType,    // optional, default: $defaultReturnType
    $alias          // optional, default null
);

$router->post('/login', Controllers\Login::class, Router::REDIRECT);
$router->get('/dashboard', Controllers\Dashboard::class, Router::HTML, 'dashboard');
$router->get('/api/user/{id}', Controllers\Api\Users::class, Router::JSON);
$router->get('/user/{id:\d+}/{box}', Controllers\Api\Users\Inbox::class, Router::HTML, 'user_home');

Get requested route info and aliases., (*7)


$routeInfo = $router->getRoute(); // Returns Selami\Router\Route object.

Now you can call your controller., (*8)

$controller = new {$routeInfo->getController()}($routeInfo->getUriParameters());

$outputMethod = 'return' . ucfirst($routeInfo->getReturnType());

echo $controller->$outputMethod($request, $response);

Our sample Controller class can be like:, (*9)

<?php

declare(strict_types=1);

namespace Controller\Api\Users;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use BaseController;

class Inbox extends BaseController {

    private $uriParameters;

    public function __construct($uriParameters) 
    {
        parent::__construct();
        $this->uriParameters = $uriParameters;
    }

    public function returnHTML(ServerRequestInterface $request, ResponseInterface $response)
    {
        $response = $this->response->withHeader('Content-Type', 'text/html');
        $response->getBody()->write('Your user id is: ' . $this->args['id'] . '. You are viewing your '. $this->uriParameters['box']);
        return $response->output();
    }
}

$route value syntax when adding get, post, put etc routes.

See FastRoute documentation., (*10)

The Versions

11/07 2018

dev-master

9999999-dev https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

11/07 2018

0.7

0.7.0.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

16/07 2017

0.6.1

0.6.1.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

14/07 2017

0.6

0.6.0.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

02/07 2017

0.5

0.5.0.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

30/06 2017

0.4

0.4.0.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

17/06 2017

0.3.1

0.3.1.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

17/06 2017

0.3

0.3.0.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

23/03 2017

0.2.1

0.2.1.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller class name to be instantiated.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

03/01 2017

0.2

0.2.0.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller and action.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami

15/12 2016

0.1

0.1.0.0 https://github.com/selamiphp/router

Router and Dispatcher built on top nikic/FastRoute that returns controller and action.

  Sources   Download

MIT

The Requires

 

The Development Requires

router fastroute selami