PHP code example of codeinc / psr15-router-middleware

1. Go to this page and download the library: Download codeinc/psr15-router-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codeinc / psr15-router-middleware example snippets



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);