Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
/ router Public archive

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.

License

Notifications You must be signed in to change notification settings

selamiphp/router

Repository files navigation

Selami Router

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Codacy Badge

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.

Installation

Install this library using composer:

$ composer require selami/router

Example

Get global values.

<?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.

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

Add routes that expect HTTP request methods. $route variable uses nikic/FastRoute's route syntax.

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.

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

Now you can call your controller.

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

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

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

Our sample Controller class can be like:

<?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.

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages