by Enrico Zimuel / @ezimuel
Senior Software Engineer
Zend Technologies, a Rogue Wave Company
|
A function that gets a request and generates a response
function ($request, $response) {
// manipulate $request to generate a $response
return $response;
}
1) Passing middleware to other middleware:
class Middleware
{
protected $middleware;
public function __construct(callable $middleware) {
$this->middleware = $middleware;
}
public function __invoke($request, $response) {
// do something before
call_user_func($this->middleware, $request, $response);
// do something after
}
}
2) Use an additional callable during the invoke ($next)
class Middleware
{
public function __invoke($request, $response, callable $next = null) {
// do something before
if ($next) {
$next($request, $response);
}
// do something after
}
}
GET /path HTTP/1.1
Host: example.com
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
{"foo":"bar"}
But every framework does it differently.
$method = $request->getMethod();
$method = $request->getRequestMethod();
$method = $request->method;
$method = $request->getMethod();
$accept = $request->getHeader('Accept');
$path = $request->getUri()->getPath();
$controller = $request->getAttribute('controller');
$response->getBody()->write('Hello world!');
$response = $response
->withStatus(200, 'OK')
->withHeader('Content-Type', 'text/plain');
function ($request, $response, $next)
use Zend\Expressive\AppFactory;
chdir(dirname(__DIR__));
require 'vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function ($request, $response, $next) {
$response->getBody()->write('Hello, world!');
return $response;
});
$app->pipeRoutingMiddleware();
$app->pipeDispatchMiddleware();
$app->run();
namespace Zend\Expressive\Router;
use Psr\Http\Message\ServerRequestInterface as Request;
interface RouterInterface
{
public function addRoute(Route $route);
public function match(Request $request);
public function generateUri($name, array $substitutions = []);
}
// $app is an instance of Zend\Expressive\AppFactory
$app->get('/', function ($request, $response, $next) {
$response->getBody()->write('Hello, world!');
return $response;
});
// $app is an instance of Zend\Expressive\AppFactory
// Executed in all the requests
$app->pipe($apiMiddleware);
$app->pipe('middleware service name');
// Pipe to a specific URL
$app->pipe('/api', $apiMiddleware);
$app->pipe('/api', 'middleware service name');
// Error handler
$app->pipeErrorHandler('error handler service name');
$app->pipeErrorHandler('/api', 'error handler service name');
use Zend\Expressive\AppFactory;
use Zend\ServiceManager\ServiceManager;
$container = new ServiceManager();
$container->setFactory('HelloWorld', function ($container) {
return function ($req, $res, $next) {
$res->write('Hello, world!');
return $res;
};
});
$app = AppFactory::create($container);
$app->get('/', 'HelloWorld');
We support container-interop
namespace Zend\Expressive\Template;
interface TemplateRendererInterface
{
public function render($name, $params = []);
public function addPath($path, $namespace = null);
public function getPaths();
public function addDefaultParam($templateName, $param, $value);
}
use Zend\Expressive\Application;
use Zend\Expressive\Plates\PlatesRenderer;
use Zend\Expressive\TemplatedErrorHandler;
$plates = new PlatesRenderer();
$plates->addPath(__DIR__ . '/templates/error', 'error');
$finalHandler = new TemplatedErrorHandler(
$plates,
'error::404',
'error::500'
);
$app = new Application($router, $container, $finalHandler);
$ composer create-project zendframework/zend-expressive-skeleton <path>
$ composer serve
You can browse to http://localhost:8080
A brief look at the source code...
Rate this talk at joind.in/talk/f6938
More info: zendframework.github.io/zend-expressive
Contact me: enrico [at] zend.com
Follow me: @ezimuel
This work is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
I used reveal.js to make this presentation.