PHP code example of friendsofhyperf / http-dispatcher-enhance

1. Go to this page and download the library: Download friendsofhyperf/http-dispatcher-enhance 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/ */

    

friendsofhyperf / http-dispatcher-enhance example snippets




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);
    }
}


// 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]]);