PHP code example of inhere / middleware

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

    

inhere / middleware example snippets


composer 

function func_middleware($request, RequestHandlerInterface $handler)
{
    echo ">>> 0 before\n";
    $res = $handler->handle($request);
    echo "0 after >>>\n";

    return $res;
}

function func_middleware1($request, RequestHandlerInterface $handler)
{
    echo ">>> n before \n";
    $res = $handler->handle($request);
    echo "n after >>>\n";

    return $res;
}

$chain = new MiddlewareStack([
    'func_middleware',
    function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
        echo ">>> 1 before \n";
        $res = $handler->handle($request);
        $res->getBody()->write(' + node 1');
        echo "1 after >>> \n";
        return $res;
    },
    function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
        echo ">>> 2 before \n";
        $res = $handler->handle($request);
        $res->getBody()->write(' + node 2');
        echo "2 after >>> \n";
        return $res;
    },
    function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
        echo ">>> 3 before \n";
        $res = $handler->handle($request);
        $res->getBody()->write(' + node 3');
        echo "3 after >>> \n";
        return $res;
    },
    'func_middleware1'
]);

$request = HttpFactory::createServerRequest('GET', 'http://www.abc.com/home');

$chain->setCoreHandler(function (ServerRequestInterface $request) {
    echo " (THIS IS CORE)\n";

    return HttpFactory::createResponse()->write('-CORE-');
});

$res = $chain($request);

echo PHP_EOL . 'response content: ', (string)$res->getBody() . PHP_EOL;

use PhpComp\Http\Message\HttpFactory;
use PhpComp\Http\Message\HttpUtil;
use Inhere\Middleware\MiddlewareStackAwareTrait;
use Inhere\Route\RouterInterface;
use Inhere\Route\Router;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;


$app = new class implements RequestHandlerInterface {
    use MiddlewareStackAwareTrait;

    /**
     * @var Router
     */
    private $router;

    public function run(ServerRequestInterface $request)
    {
        $response = $this->callStack($request);

        HttpUtil::respond($response);
    }

    /**
     * 在这里处理请求返回响应对象
     * @param ServerRequestInterface $request
     * @return ResponseInterface
     * @throws Throwable
     */
    public function handleRequest(ServerRequestInterface $request): ResponseInterface
    {
        $method = $request->getMethod();
        $uriPath = $request->getUri()->getPath();
        $response = HttpFactory::createResponse();

        try {
            // $this->router->match($uriPath, $method);
            $result = $this->router->dispatch(null, $uriPath, $method);
            $response->getBody()->write($result);
        } catch (Throwable $e) {
            $response->getBody()->write($e->getTraceAsString());
        }

        return $response;
    }

    /**
     * @return RouterInterface
     */
    public function getRouter(): RouterInterface
    {
        return $this->router;
    }

    /**
     * @param RouterInterface $router
     */
    public function setRouter(RouterInterface $router)
    {
        $this->router = $router;
    }
};

$router = new Inhere\Route\Router();

/**
 * add routes
 */
$router->get('/', function () {
   echo 'hello, world';
});

$router->get('/hello/{name}', function ($args) {
    echo "hello, {$args['name']}";
});


/**
 * add middleware
 */
$app->use(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
    echo 'before handle0 > ';
    $res = $handler->handle($request);
    echo ' > after handle0';

    return $res;
});

$app->use(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
    echo 'before handle1 > ';
    $res = $handler->handle($request);
    echo ' > after handle1';

    return $res;
});

/**
 * run
 */
$req = HttpFactory::createServerRequestFromArray($_SERVER);

$app->setRouter($router);
$app->run($req);
bash
$ php -S 127.0.0.1:8009 examples/app.php