friendsofhyperf / http-dispatcher-enhance
Http dispatcher enhance for hyperf.
Fund package maintenance!
huangdijia
hdj.me/sponsors
Requires
- hyperf/dispatcher: ~2.2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- hyperf/http-server: ~2.2.0
- phpstan/phpstan: ^1.0
README
Http Dispatcher Enhance component for Hyperf.
Installation
composer require "friendsofhyperf/http-dispatcher-enhance"
Usage
- Define a middleware
<?php declare(strict_types=1); namespace App\Middleware\Auth; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class FooMiddleware implements MiddlewareInterface { /** * @var ContainerInterface */ protected $container; /** * @var RequestInterface */ protected $request; /** * @var HttpResponse */ protected $response; public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request) { $this->container = $container; $this->response = $response; $this->request = $request; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler, $value1, $value2): ResponseInterface { var_dump($value1, $value2); // Suggestion: set a default for $value1 and $value2. return $handler->handle($request); } }
- Set middleware parameters in route definition
<?php // config/routes.php use App\Middleware\FooMiddleware; use Hyperf\HttpServer\Router\Router; Router::get('/', [\App\Controller\IndexController::class, 'index'], ['middleware' => [FooMiddleware::class, 1, 2]]); Router::get('/', [\App\Controller\IndexController::class, 'index1'], ['middleware' => [FooMiddleware::class, 3, 4]]);
Not support
@middleware
and@middlewares
annotation yet.