项目作者: teodoroleckie

项目描述 :
⚡ Simple and fast router PSR-7, PSR-17, PSR-15
高级语言: PHP
项目地址: git://github.com/teodoroleckie/router.git
创建时间: 2021-04-20T12:41:04Z
项目社区:https://github.com/teodoroleckie/router

开源协议:MIT License

下载


Router:

Latest Version on Packagist
Scrutinizer Code Quality
Build Status
Total Downloads

Simple and fast router PSR-7, PSR-17, PSR-15

Installation

You can install the package via composer:

  1. composer require tleckie/router

Usage

  1. <?php
  2. use HttpSoft\Emitter\SapiEmitter;
  3. use HttpSoft\Message\ResponseFactory;
  4. use HttpSoft\Message\ServerRequestFactory;
  5. use Psr\Http\Message\ResponseInterface;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use Psr\Http\Server\RequestHandlerInterface;
  8. use Tleckie\Router\Router;
  9. $router = new Router(
  10. new ServerRequestFactory(),
  11. new ResponseFactory(),
  12. new SapiEmitter()
  13. );
  14. /**
  15. * Class UserController
  16. *
  17. * @author Teodoro Leckie Westberg <teodoroleckie@gmail.com>
  18. */
  19. class UserController
  20. {
  21. /**
  22. * @param ServerRequestInterface $request
  23. * @param ResponseInterface $response
  24. * @param array $params
  25. * @return ResponseInterface
  26. */
  27. public function retrieveUserAction(ServerRequestInterface $request, ResponseInterface $response, array $params): ResponseInterface
  28. {
  29. $response->getBody()->write(sprintf(" CONTROLLER USER ID: %s", $params['id']));
  30. return $response->withHeader('Content-Type', 'text/html');
  31. }
  32. }
  33. // routing with middleware
  34. $router->get('/user/(?<id>[0-9]+)/',
  35. static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
  36. $response = $handler->handle($request);
  37. $response->getBody()->write(sprintf(' (ROUTES MIDDLEWARE ID: #%s) ', $params['id']));
  38. return $response;
  39. },
  40. [new UserController, 'retrieveUserAction']
  41. );

Add global middleware

The middleware added in the add method is always executed.

  1. <?php
  2. require_once "vendor/autoload.php";
  3. use HttpSoft\Emitter\SapiEmitter;
  4. use HttpSoft\Message\ResponseFactory;
  5. use HttpSoft\Message\ServerRequestFactory;
  6. use Psr\Http\Message\ResponseInterface;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. use Psr\Http\Server\MiddlewareInterface;
  9. use Psr\Http\Server\RequestHandlerInterface;
  10. use Tleckie\Router\Router;
  11. $router = new Router(
  12. new ServerRequestFactory(),
  13. new ResponseFactory(),
  14. new SapiEmitter()
  15. );
  16. /**
  17. * Class ExampleMiddleware
  18. * @author Teodoro Leckie Westberg <teodoroleckie@gmail.com>
  19. */
  20. class ExampleMiddleware implements MiddlewareInterface
  21. {
  22. /**
  23. * <strong>Objects that implement MiddlewareInterface will not receive routing parameters</strong>
  24. * @param ServerRequestInterface $request
  25. * @param RequestHandlerInterface $handler
  26. * @return ResponseInterface
  27. */
  28. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  29. {
  30. $response = $handler->handle($request);
  31. $response->getBody()->write(' (GLOBAL 1)');
  32. return $response;
  33. }
  34. }
  35. // Add app closure middleware. The closure middleware will receive the routing parameters
  36. $router->add(static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params){
  37. $response = $handler->handle($request);
  38. $response->getBody()->write(sprintf(' (GLOBAL ID: %s)', $params['id']));
  39. return $response;
  40. });
  41. // app middleware.
  42. $router->add(new ExampleMiddleware());

Support for all methods.

  1. <?php
  2. require_once "vendor/autoload.php";
  3. use HttpSoft\Emitter\SapiEmitter;
  4. use HttpSoft\Message\ResponseFactory;
  5. use HttpSoft\Message\ServerRequestFactory;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use Psr\Http\Server\RequestHandlerInterface;
  8. use Tleckie\Router\Router;
  9. $router = new Router(
  10. new ServerRequestFactory(),
  11. new ResponseFactory(),
  12. new SapiEmitter()
  13. );
  14. $router->all('/user/(?<id>[0-9]+)/',
  15. static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
  16. $response = $handler->handle($request);
  17. $response->getBody()->write(' (ROUTES MIDDLEWARE #1#)' . $params['id']);
  18. return $response;
  19. },
  20. static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
  21. $response = $handler->handle($request);
  22. $response->getBody()->write(' (ROUTES MIDDLEWARE #2#)' . $params['id']);
  23. return $response;
  24. },
  25. [new UserController, 'retrieveUserAction']
  26. );
  1. <?php
  2. require_once "vendor/autoload.php";
  3. use HttpSoft\Emitter\SapiEmitter;
  4. use HttpSoft\Message\ResponseFactory;
  5. use HttpSoft\Message\ServerRequestFactory;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use Psr\Http\Server\RequestHandlerInterface;
  8. use Tleckie\Router\Exception\RouteNotFoundException;
  9. use Tleckie\Router\Router;
  10. $router = new Router(
  11. new ServerRequestFactory(),
  12. new ResponseFactory(),
  13. new SapiEmitter()
  14. );
  15. $router->post('/user/(?<id>[0-9]+)/',
  16. static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
  17. $response = $handler->handle($request);
  18. $response->getBody()->write(' (ROUTES MIDDLEWARE #1#)' . $params['id']);
  19. return $response;
  20. },
  21. static function (ServerRequestInterface $request, RequestHandlerInterface $handler, array $params) {
  22. $response = $handler->handle($request);
  23. $response->getBody()->write(' (ROUTES MIDDLEWARE #2#)' . $params['id']);
  24. return $response;
  25. },
  26. [new UserController, 'retrieveUserAction']
  27. );
  28. try {
  29. $router->run(
  30. $_SERVER['REQUEST_METHOD'],
  31. $_SERVER['REDIRECT_URL'] ?? '/'
  32. );
  33. } catch (RouteNotFoundException $exception) {
  34. // handle 404
  35. }

Methods:

GET, POST, HEAD, PATCH, OPTIONS, DELETE, PUT,