PHP code example of raylin666 / middleware

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

    

raylin666 / middleware example snippets




use Raylin666\Middleware\Middleware;
use Psr\Http\Message\ServerRequestInterface;
use Raylin666\Middleware\DelegateInterface;
use Raylin666\Middleware\Dispatcher;
use Raylin666\Http\Request;

class ChlidMiddleware extends Middleware
{
    public function handler(ServerRequestInterface $request, DelegateInterface $next)
    {
        // TODO: Implement handle() method.
        dump($request);
        // 将控制权委托给下一个中间件
        return $next->process($request);
        /*
         * Dispatcher分发器将会按顺序执行中间件。但需要注意,不是分发器注册的中间件都必须会执行,只有当中间件执行通过后
         * 调用return $next->process($request) 才能继续执行下个中间件, 否则将结束中间件的继续分发。
         */
    }
}

class Chlid1Middleware extends Middleware
{
    public function handler(ServerRequestInterface $request, DelegateInterface $next)
    {
        // TODO: Implement handle() method.
        echo 2;
        return $next->process($request);
    }
}

class Chlid2Middleware extends Middleware
{
    public function handler(ServerRequestInterface $request, DelegateInterface $next)
    {
        // TODO: Implement handle() method.
        echo 3;
    }
}

$dispatcher = new Dispatcher([
    new ChlidMiddleware(),
    new Chlid1Middleware(),
    new Chlid2Middleware(),
]);

$dispatcher->dispatch(new Request('GET', '/'));