On this page
Caution
The documentation you are viewing is for an older version of this component.
Switch to the latest (v3) version.
Getting Started
Quick Start: Standalone Usage
Mezzio allows you to get started at your own pace. You can start with the simplest example, detailed below, or move on to a more structured, configuration-driven approach as detailed in the use case examples.
1. Create a new project directory
First, let's create a new project directory and enter it:
$ mkdir mezzio
$ cd mezzio
2. Install Mezzio
If you haven't already, install Composer. Once you have, we can install Mezzio, along with a router and a container:
$ composer require mezzio/mezzio mezzio/mezzio-fastroute laminas/laminas-servicemanager
Routers
Mezzio needs a routing implementation in order to create routed middleware. We suggest FastRoute in the quick start, but you can also currently choose from Aura.Router and laminas-router.
Containers
We highly recommend using dependency injection containers with Mezzio; they allow you to define dependencies for your middleware, as well as to lazy load your middleware only when it needs to be executed. We suggest laminas-servicemanager in the quick start, but you can also use any container supporting container-interop.
3. Create a web root directory
You'll need a directory from which to serve your application, and for security
reasons, it's a good idea to keep it separate from your source code. We'll
create a public/
directory for this:
$ mkdir public
4. Create your bootstrap script
Next, we'll create a bootstrap script. Such scripts typically set up the
environment, set up the application, and invoke it. This needs to be in our web
root, and we want it to intercept any incoming request; as such, we'll use
public/index.php
:
<?php
use Mezzio\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();
Rewriting URLs
Many web servers will not rewrite URLs to the bootstrap script by default. If you use Apache, for instance, you'll need to set up rewrite rules to ensure your bootstrap is invoked for unknown URLs. We'll cover that in a later chapter.
Routing and dispatching
Note the lines from the above:
$app->pipeRoutingMiddleware(); $app->pipeDispatchMiddleware();
Mezzio's
Application
class provides two separate middlewares, one for routing, and one for dispatching middleware matched by routing. This allows you to slip in validations between the two activities if desired. They are not automatically piped to the application, however, to allow exactly that situation, which means they must be piped manually.
5. Start a web server
Since we're just testing out the basic functionality of our application, we'll use PHP's 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!
Tip: Serve via Composer
To simplify starting up a local web server, try adding the following to your
composer.json
:"scripts": { "serve": "php -S 0.0.0.0:8080 -t public/" }
Once you've added that, you can fire up the web server using:
$ composer serve
Next steps
At this point, you have a working mezzio application, that responds to a single route. From here, you may want to read up on:
Additionally, we have more use case examples.