Today, we are pleased to announce the immediate availability of the first release candidate of Expressive, a PSR-7 middleware microframework.
PSR-7 is a standard defining HTTP message interfaces; these are the incoming request and outgoing response for your application. By using PSR-7, we ensure that your applications will work in other PSR-7 contexts.
Middleware is any code sitting between a request and a response; it typically analyzes the request to aggregate incoming data, delegates it to another layer to process, and then creates and returns a response. Middleware can and should be relegated only to those tasks, and should be relatively easy to write and maintain.
Middleware is also designed for composability; you should be able to nest middleware and re-use middleware anywhere that supports PSR-7.
With Expressive, you can build PSR-7-based middleware applications:
- Single Page Applications
- and more.
Expressive builds on zend-stratigility, our middleware foundation, to provide a robust convenience layer on which to build applications. The features it provides include:
Stratigility provides limited, literal matching only. Expressive allows you to utilize dynamic routing capabilities from a variety of routers, providing much more fine-grained matching capabilities. The routing layer also allows restricting matched routes to specific HTTP methods, and will return "405 Not Allowed" responses with an "Allow" HTTP header containing allowed HTTP methods for invalid requests.
Routing is abstracted in Expressive, allowing the developer to choose the routing library that best fits the project needs. By default, we provide wrappers for Aura.Router, FastRoute, and the zend-mvc router, and recommend using FastRoute.
Expressive encourages the use of Dependency Injection, and defines its
Applicationclass to compose a container-interop
ContainerInterfaceinstance. The container is used to lazy-load middleware, whether it is piped (Stratigility interface) or routed (Expressive). We provide instructions for using Aura.Di, Pimple, and zend-servicemanager, recommending the latter.
While Expressive does not assume templating is being used, it provides a templating abstraction. Developers can write middleware that typehints on this abstraction, and assume that the underlying adapter will provide layout support and namespaced template support. We provide support for Plates, Twig, and zend-view.
- Error Handling
Applications should handle errors gracefully, but also handle them differently in development versus production. Expressive provides both basic error handling via Stratigility’s own
FinalHandlerimplementation, as well as more advanced error handling via two specialized error handlers: a templated error handler for production, and a whoops-based error handler for development.
The easiest way to get started with Expressive is to use the skeleton application and installer. The skeleton provides a generic structure for creating your applications, and prompts you to choose a router, dependency injection container, template renderer, and error handler from the outset.
1. Create a new project
First, we’ll create a new project, using Composer’s
$ composer create-project -s rc zendframework/zend-expressive-skeleton expressive
This will prompt you to choose:
- A router. We recommend using the default, FastRoute.
- A dependency injection container. We recommend using the default, Zend ServiceManager.
- A template renderer. You can ignore this when creating an API project, but if you will be creating any HTML pages, we recommend installing one. We prefer Plates.
- An error handler. Whoops is a very nice option for development, as it gives you extensive, browseable information for exceptions and errors raised.
2. Start a web server
The Skeleton + Installer creates a full application structure that’s ready-to-go when complete. You can test it out using built-in web server.
From the project root directory, execute the following:
$ php -S 0.0.0.0:8080 -t public/
This starts up a web server on localhost port 8080; browse to http://localhost:8080/ to see if your application responds correctly!
From here, it’s time to start writing middleware!
Welcome to the new era of PHP!