1. Go to this page and download the library: Download ellipse/router-fastroute 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/ */
ellipse / router-fastroute example snippets
namespace App;
use FastRoute\RouteParser;
use FastRoute\DataGenerator;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use Ellipse\Router\FastRouteRequestHandler;
// Get a psr7 request.
$request = some_psr7_request_factory();
// Create a fastroute dispatcher factory.
$factory = function ($r) {
// Create a new fastroute route collector.
$r = new RouteCollector(
new RouteParser\Std, new DataGenerator\GroupCountBased
);
// The route handlers must be Psr-15 request handlers.
$r->get('/', new SomeRequestHandler);
// When this route is matched a new request with an 'id' attribute would be passed to the request handler.
$r->get('/path/{id}', new SomeOtherRequestHandler);
// return a fastroute dispatcher
return new Dispatcher\GroupCountBased($r->getData());
};
// Create a fastroute request handler using this factory.
$handler = new FastRouteRequestHandler($factory);
// When a route is matched the request is handled by this route request handler.
// Otherwise NotFoundException or MethodNotAllowedException is thrown
$response = $handler->handle($request);
namespace App;
use FastRoute\RouteParser;
use FastRoute\DataGenerator;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use Ellipse\Router\FastRouteMiddleware;
// Get a psr7 request.
$request = some_psr7_request_factory();
// Create a fastroute dispatcher factory.
$factory = function ($r) {
// Create a new fastroute route collector.
$r = new RouteCollector(
new RouteParser\Std, new DataGenerator\GroupCountBased
);
// The route handlers must be Psr-15 request handlers.
$r->get('/', new SomeRequestHandler);
// When this route is matched a new request with an 'id' attribute would be passed to the request handler.
$r->get('/path/{id}', new SomeOtherRequestHandler);
// return a fastroute dispatcher
return new Dispatcher\GroupCountBased($r->getData());
};
// Create a fastroute middleware using this factory.
$middleware = new FastRouteMiddleware($factory);
// When a route is matched the request is handled by this route request handler.
// When a NotFoundException is thrown, NextRequestHandler is used to handle the request.
$response = $middleware->process($request, new NextRequestHandler);
namespace App;
use Ellipse\Router\FastRoute\GroupCountBasedMiddleware;
use Ellipse\Router\FastRoute\GroupCountBasedRequestHandler;
// Create a route definition callback.
$routeDefinitionCallback = function ($r) {
// The route handlers must be Psr-15 request handlers.
$r->get('/', new SomeRequestHandler);
// When this route is matched a new request with an 'id' attribute would be passed to the request handler.
$r->get('/path/{id}', new SomeOtherRequestHandler);
};
// Create a fastroute middleware using a group count based dispatcher.
$middleware = new GroupCountBasedMiddleware($routeDefinitionCallback);
// Create a fastroute request handler using a group count based dispatcher.
$handler = new GroupCountBasedRequestHandler($routeDefinitionCallback);
namespace App;
use Ellipse\Router\FastRouteMiddleware;
use Ellipse\Router\FastRouteRequestHandler;
use Ellipse\Router\FastRoute\SimpleDispatcher;
// Create a route definition callback like with fastroute simpleDispatcher function.
$routeDefinitionCallback = function ($r) {
// The route handlers must be Psr-15 request handlers.
$r->get('/', new SomeRequestHandler);
// When this route is matched a new request with an 'id' attribute would be passed to the request handler.
$r->get('/path/{id}', new SomeOtherRequestHandler);
};
// An optional array of options can be passed like with fastroute simpleDispatcher function.
$options = [
// Can specify fastroute classes to use.
];
// Create a dispatcher factory using fastroute simpleDispatcher function.
// Same with CachedDispatcher using fastroute cachedDispatcher.
$factory = new SimpleDispatcher($routeDefinitionCallback, $options);
// Create a fastroute middleware using this dispatcher factory.
$middleware = new FastRouteMiddleware($factory);
// Create a fastroute request handler using this dispatcher factory.
$handler = new FastRouteRequestHandler($factory);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.