Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if (!$request instanceof ActionRequest) {
         $request = new ActionRequest($request);
     }
     $response = $next($request, $response);
     if (function_exists('bzcompress') and $request->acceptsEncoding('bzip2')) {
         $response = $response->withHeader('Content-Encoding', 'bzip2');
         $this->server->setCompression('bzip2');
     } elseif (function_exists('gzencode') and $request->acceptsEncoding('gzip')) {
         $response = $response->withHeader('Content-Encoding', 'gzip');
         $this->server->setCompression('gzip');
     }
     return $response;
 }
Beispiel #2
0
<?php

use Jivoo\Http\ActionRequest;
use Jivoo\Http\Route\CallableRoute;
use Jivoo\Http\Route\CallableScheme;
use Jivoo\Http\Router;
use Jivoo\Http\SapiServer;
use Psr\Http\Message\ResponseInterface;
require '../vendor/autoload.php';
$router = new Router();
$router->addScheme(new CallableScheme());
$router->root(function (ActionRequest $request, ResponseInterface $response, $parameters) {
    $response->getBody()->write('Hello, World');
    return $response;
});
$router->error(function (ActionRequest $request, ResponseInterface $response, $parameters) {
    $response->getBody()->write('Page not found');
    return $response;
});
$router->match('foo', function (ActionRequest $request, ResponseInterface $response, $parameters) {
    $response->getBody()->write('Foo');
    return $response;
});
$router->match('foo/:bar', function (ActionRequest $request, ResponseInterface $response, $parameters) {
    $response->getBody()->write('Foo: ' . $parameters['bar']);
    return $response;
});
$server = new SapiServer($router);
$server->listen();