friendsofhyperf/http-dispatcher-enhance

Http dispatcher enhance for hyperf.

1.0.0 2022-04-21 00:27 UTC

This package is auto-updated.

Last update: 2024-12-14 11:12:34 UTC


README

Latest Test Latest Stable Version Total Downloads GitHub license

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.