PHP code example of alexpts / php-psr15-routing

1. Go to this page and download the library: Download alexpts/php-psr15-routing 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/ */

    

alexpts / php-psr15-routing example snippets


use PTS\PSR15Routing\CallableAdapter;
use PTS\PSR15Routing\Router;
use PTS\PSR15Routing\Route;
use PTS\PSR15Routing\RouterMiddleware;

$router = new Router;

$router->add('/', new Route('/', new CallableAdapter(function($request) {
    return $response; // $response must be ResponseInterface
})));

$handler = ...; // $handler bust be RequestHandlerInterface
$router->add('/admin', new Route('/', $handler));


// with priority
$router->add('/admin/1', new Route('/', $handler), 100);

// some middleware manager/runner
$app = (new MiddlewareManager)
	->push(new RouterMiddleware($router);

$response = $app->handler($request);

use PTS\PSR15Routing\CallableAdapter;
use PTS\PSR15Routing\Router;
use PTS\PSR15Routing\Route;
use PTS\PSR15Routing\RouterMiddleware;
use PTS\EndPoint\DynamicPoint;
use PTS\EndPoint\EndPoint;

$router = new Router;

$flexHandler = new DynamicPoint([
	'prefix' => '\\Namespace\\ForDynamicController\\'
]);
$router->add('flex', new Route('/{_controller}/{_action}/', $flexHandler));
// /blog/get/ => \\Namespace\\ForDynamicController\\Blog::get()

$mainPageHandler = new EndPoint(['controller' => SomeController::class]);
$router->add('mainPage', new Route('/', $mainPageHandler)); // SomeController::index()

// with params from url
$postHandler = new DynamicPoint([
	'controller' => '\\Project\\PostController',
]);
$router->add('posts', new Route('/posts/{_action}/{id}/', $postHandler));
// /posts/get/4/ => PostController::get($id = 4)
composer