codeinc/psr15-router-middleware

This package is abandoned and no longer maintained. The author suggests using the codeinc/router package instead.

A PSR-15 router middleware

2.0.2 2018-08-30 06:53 UTC

This package is not auto-updated.

Last update: 2022-02-01 13:13:51 UTC


README

This library is a very simple PSR-15 controller router middleware written in PHP 7.1.

Usage

<?php
use CodeInc\Psr15RouterMiddleware\RouterMiddleware;
use CodeInc\Psr15RouterMiddleware\AbstractController;
use CodeInc\Psr7Responses\HtmlResponse;
use Psr\Http\Message\ResponseInterface;

class HomePage extends AbstractController 
{
    public static function getUriPath():string { return '/'; }   
    public function process():ResponseInterface { return new HtmlResponse("<h1>Hello world!</h1>"); }
}
class AnotherPage extends AbstractController
{
    public static function getUriPath():string { return '/another-page.html'; }   
    public function process():ResponseInterface { return new HtmlResponse("<h1>Another page</h1>"); }
}
class NotFound extends AbstractController
{
    public static function getUriPath():string { return '/error404.html'; }   
    public function process():ResponseInterface { return new HtmlResponse("<h1>Page not found</h1>"); }
}

$router = new RouterMiddleware();
$router->registerControllerClass(HomePage::class);
$router->registerControllerClass(AnotherPage::class);
$router->setNotFoundControllerClass(NotFound::class);

Installation

This library is available through Packagist and can be installed using Composer:

composer require codeinc/psr15-router-middleware

License

This library is published under the MIT license (see the LICENSE file).