1. Go to this page and download the library: Download tleckie/router 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/ */
tleckie / router example snippets
use HttpSoft\Emitter\SapiEmitter;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\ServerRequestFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Tleckie\Router\Router;
$router = new Router(
new ServerRequestFactory(),
new ResponseFactory(),
new SapiEmitter()
);
/**
* Class UserController
*
* @author Teodoro Leckie Westberg <[email protected]>
*/
class UserController
{
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param array $params
* @return ResponseInterface
*/
public function retrieveUserAction(ServerRequestInterface $request, ResponseInterface $response, array $params): ResponseInterface
{
$response->getBody()->write(sprintf(" CONTROLLER USER ID: %s", $params['id']));
return $response->withHeader('Content-Type', 'text/html');
}
}
// routing with middleware
$router->get('/user/(?<id>[0-9]+)/',
static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
$response = $handler->handle($request);
$response->getBody()->write(sprintf(' (ROUTES MIDDLEWARE ID: #%s) ', $params['id']));
return $response;
},
[new UserController, 'retrieveUserAction']
);
ttpSoft\Emitter\SapiEmitter;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\ServerRequestFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Tleckie\Router\Router;
$router = new Router(
new ServerRequestFactory(),
new ResponseFactory(),
new SapiEmitter()
);
/**
* Class ExampleMiddleware
* @author Teodoro Leckie Westberg <[email protected]>
*/
class ExampleMiddleware implements MiddlewareInterface
{
/**
* <strong>Objects that implement MiddlewareInterface will not receive routing parameters</strong>
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
$response->getBody()->write(' (GLOBAL 1)');
return $response;
}
}
// Add app closure middleware. The closure middleware will receive the routing parameters
$router->add(static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params){
$response = $handler->handle($request);
$response->getBody()->write(sprintf(' (GLOBAL ID: %s)', $params['id']));
return $response;
});
// app middleware.
$router->add(new ExampleMiddleware());
ttpSoft\Emitter\SapiEmitter;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\ServerRequestFactory;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Tleckie\Router\Router;
$router = new Router(
new ServerRequestFactory(),
new ResponseFactory(),
new SapiEmitter()
);
$router->all('/user/(?<id>[0-9]+)/',
static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
$response = $handler->handle($request);
$response->getBody()->write(' (ROUTES MIDDLEWARE #1#)' . $params['id']);
return $response;
},
static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
$response = $handler->handle($request);
$response->getBody()->write(' (ROUTES MIDDLEWARE #2#)' . $params['id']);
return $response;
},
[new UserController, 'retrieveUserAction']
);
ttpSoft\Emitter\SapiEmitter;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\ServerRequestFactory;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Tleckie\Router\Exception\RouteNotFoundException;
use Tleckie\Router\Router;
$router = new Router(
new ServerRequestFactory(),
new ResponseFactory(),
new SapiEmitter()
);
$router->post('/user/(?<id>[0-9]+)/',
static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
$response = $handler->handle($request);
$response->getBody()->write(' (ROUTES MIDDLEWARE #1#)' . $params['id']);
return $response;
},
static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
$response = $handler->handle($request);
$response->getBody()->write(' (ROUTES MIDDLEWARE #2#)' . $params['id']);
return $response;
},
[new UserController, 'retrieveUserAction']
);
try {
$router->run(
$_SERVER['REQUEST_METHOD'],
$_SERVER['REDIRECT_URL'] ?? '/'
);
} catch (RouteNotFoundException $exception) {
// handle 404
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.