by Enrico Zimuel
Senior Software Engineer
Rogue Wave Software (USA)
phpDay 2018, Verona (Italy), 12th May
|
The PHP framework for middleware applications
use Zend\Diactoros\Response\JsonResponse;
use Zend\Expressive\Application;
$container = require 'config/container.php';
$app = $container->get(Application::class);
$app->pipe('/api/ping', function($request) {
return new JsonResponse(['ack' => time()]);
});
// or $app->pipe('/api/ping', \App\Handler\PingHandler::class);
$app->run();
use Psr\Http\Message\ResponseInterface; // PSR-7
use Psr\Http\Message\ServerRequestInterface; // PSR-7
use Psr\Http\Server\RequestHandlerInterface; // PSR-15
use Zend\Diactoros\Response\JsonResponse;
class PingHandler implements RequestHandlerInterface
{
public function handle(
ServerRequestInterface $request
) : ResponseInterface
{
return new JsonResponse(['ack' => time()]);
}
}
use Psr\Http\Message\ResponseInterface; // PSR-7
use Psr\Http\Message\ServerRequestInterface; // PSR-7
use Psr\Http\Server\MiddlewareInterface; // PSR-15
use Psr\Http\Server\RequestHandlerInterface; // PSR-15
class AuthMiddleware implements MiddlewareInterface
{
// ...
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
) : ResponseInterface
{
$user = $this->auth->authenticate($request);
if (null !== $user) {
return $handler->handle($request->withAttribute(
UserInterface::class,
$user
));
}
return $this->auth->unauthorizedResponse($request);
}
}
$app->route('/api/users[/{id}]', [
Authentication\AuthenticationMiddleware::class,
Authorization\AuthorizationMiddleware::class,
Api\Action\UserAction::class
], ['GET', 'POST', 'PATCH', 'DELETE'], 'api.users');
// or route each HTTP method
$app->get('/api/users[/{id}]', ..., 'api.users.get');
$app->post('/api/users', ..., 'api.users.post');
$app->patch('/api/users/{id}', ..., 'api.users.patch');
$app->delete('/api/users/{id}', ..., 'api.users.delete');
You can start using Expressive with composer:
composer create-project zendframework/zend-expressive-skeleton <dir>
pecl install swoole
$http = new swoole_http_server("127.0.0.1", 9501);
$http->on("start", function ($server) {
echo "Started at http://127.0.0.1:9501\n";
});
$http->on("request", function ($request, $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello World\n");
});
$http->start();
Use wshafer/swoole-expressive library
Install:
composer require wshafer/swoole-expressive:dev-master
Usage:
vendor/bin/swoole-expressive --host=0.0.0.0 --port=8080
use Swoole\Http\Request;
use Swoole\Http\Response
$http = new swoole_http_server($host, $port);
$http->on(
'request',
function (Request $request, Response $response) use
($app, $psr7Request, $swooleResponse) {
$psrResponse = $app->handle($psr7Request->from($request));
$swooleResponse->fromPsr7($psrResponse, $response);
}
);
$http->start();
Expressive with Swoole runs 4x faster than nginx and Apache (source)
Rate this talk at joind.in/talk/bef90
This work is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
I used reveal.js to make this presentation.