PHP code example of weskiller / hyperf-middleware

1. Go to this page and download the library: Download weskiller/hyperf-middleware 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/ */

    

weskiller / hyperf-middleware example snippets



    
use Hyperf\Server\Event;
use Hyperf\Server\Server;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            /**
            *   
            */
            'callbacks' => [
                Event::ON_REQUEST => [Weskiller\HyperfMiddleware\Http\Server::class, 'onRequest'],
            ],
        ],
        //如果还使用了websocket
        [
            'name' => 'websocket',
            'type' => Server::SERVER_WEBSOCKET,
            /**
            *   
            */
            'callbacks' => [
                Event::ON_HAND_SHAKE => [Weskiller\HyperfMiddleware\WebSocket\Server::class, 'onHandShake'],
                Event::ON_MESSAGE => [Weskiller\HyperfMiddleware\WebSocket\Server::class, 'onMessage'],
                Event::ON_CLOSE => [Weskiller\HyperfMiddleware\WebSocket\Server::class, 'onClose'],
            ],
        ],
    ],
    'settings' => [
        /**
        *   
        */
    ],
    'callbacks' => [
        /**
        *   
        */
    ],
];


    'scan' => [
        /*
        ...
        */
        'collectors' => [
            Weskiller\HyperfMiddleware\Middleware\Collector::class,
        ]
    ],



    use App\Controller\DeveloperController;
    use App\Middleware\CommonMiddleware;
    use App\Middleware\NeedParametersMiddleware;
    use App\Middleware\NeedTwoParameterMiddleware;
    use Hyperf\HttpServer\Router\Router;
    use Weskiller\HyperfMiddleware\Middleware\Middleware;
    use Weskiller\HyperfMiddleware\Middleware\Direct;
    use Weskiller\HyperfMiddleware\Middleware\Exclude;
    use Weskiller\HyperfMiddleware\Middleware\Expect;
    
    Router::get(
        '/debug',
        [DeveloperController::class,'index'],
        [
            'middleware' => [
                CommonMiddleware::class,                                                        //default
                [NeedParametersMiddleware::class,'parameters'],                                 //with parameters
                new Middleware(NeedTwoParameterMiddleware::class,'parameter1','parameter2'),    //use instance
            ],
            Exclude::Option => [
                CommonMiddleware::class                                                         //exclude 
            ],
            Expect::Option => [
                NeedParametersMiddleware::class                                                 //expect 
            ],
            Direct::Option => ture                                                              //skip all middlewares
       ],
       );


    
    namespace App\Controller;
    
    use App\Middleware\CommonMiddleware;
    use App\Middleware\NeedParametersMiddleware;
    use App\Middleware\NeedTwoParameterMiddleware;
    use Hyperf\HttpServer\Annotation\Controller;
    use Weskiller\HyperfMiddleware\Middleware\Middleware;
    use Weskiller\HyperfMiddleware\Middleware\Direct;
    use Weskiller\HyperfMiddleware\Middleware\Exclude;
    use Weskiller\HyperfMiddleware\Middleware\Expect;

    #[Controller(prefix='developer')]
    class DeveloperController
    {
        #[RequestMapping(method:["GET"],value: 'debug')]
        #[Middleware(CommonMiddleware::class)]                                  //default
        #[ParameterMiddleware(NeedParametersMiddleware::class,"parameter")]     //with parameters
        #[ParameterMiddleware(NeedTwoParameterMiddleware::class,"parameter")]   //multiple middleware
        #[Exclude([NeedTwoParameterMiddleware::class])]                         //exclude
        #[Expect([NeedParametersMiddleware::class])]                            //expect
        #[Direct]                                                               //skip all middlewares
        public function index(ServerRequestInterface $request)
        {
            $user = $request->input('user', 'Hyperf');
            $method = $request->getMethod();
    
            return [
                'method' => $method,
                'message' => "Hello {$user}.",
            ];
        }
    }