Mezzio 101: Create an Application Using the Mezzio Skeleton Installer

Creating a site from scratch has always been a daunting task for developers. The difficulty doesn't come so much from the complex coding, but from the imprecise specifications. Even if you start with clear goals in mind, the project can grow exponentially in complexity as time goes by. New technical requirements, new features, bug fixes and refactoring all get in the way of the developer resting on his laurels.

One might argue the above are only possible in the programming world. We aren't building bridges here.

We need a reliable, well-maintained starting point for our projects. This scaffolding should be a basis that allows us to expand as needed in the future.

This will be the first article of a series meant to showcase how to build a fully-operational application using Mezzio.

Where to start?

Building the project manually is a complex operation that has too many moving parts to be accessible. Also, each of us will build it in his own image and that invites chaos even if we have the best intentions. A better solution is using the Mezzio Skeleton Installer that handles a lot of aspects automatically, the most basic being listed below:

  • Standard directory and file structure
  • Routing
  • Middleware
  • Templating
  • CI/CD

The Laminas Project is known to offer a PHP framework and component library that is kept up-to-date with the latest updates in the PHP ecosystem. A second, but highly important aspect is their focus on standards defined by the PHP Framework Interop Group.

Prerequisites

You need a working installation of PHP and Composer available on your $PATH.

What is Mezzio?

As mentioned on the Zend official site, 'Mezzio is the Laminas middleware runtime -- previously known as Expressive in the Zend framework.' Mezzio is designed around PHP-FIG, the PHP Framework Interop Group, that has defined several standards recommendations. The goal of the standards is to ensure that PHP code is written in a consistent, interoperable, and maintainable way.

What is middleware?

Middleware is code that is executed between the request and response. Most commonly, it performs these tasks, but more layers can be defined based on your application:

  • Aggregates incoming data.
  • Processes the request.
  • Creates and returns a response.

There are several advantages to using middleware:

  • It enables old systems to interface with newer ones.
  • It's scalable.
  • Integrations are created more easily.
  • It enables automating processes.

Installing Mezzio

Let's put aside the technical stuff in favor of some real work that you can be proud of. Building things with our hands, kind of.

To start, create a folder where you want your project files to be created.

mkdir -p ~/Projects/my-mezzio-project

The next command will launch the Mezzio Skeleton Installer. We will go through the installation process together, using the recommended settings. We will opt primarily for the Laminas-supported options to maintain consistency. Funnily enough, the installation should complete in less time than it takes for you to read this tutorial.

composer create-project mezzio/mezzio-skeleton ~/Projects/my-mezzio-project

You should see the text and prompt below:

step-1-installation-type

Submit 3 for option Modular.

step-2-dependency-injection

Submit 1 for option laminas-servicemanager. Just as a side note, this option is Laminas's factory-driven dependency injection container. Its version 4.0 supports PSR-11: Container interface version 1.1 and 2.

step-3-router

Submit 1 for fastroute.

step-4-templating

We chose Twig as the templating engine because of its concise syntax, extensibility and well-written documentation. laminas-view is a good choice if you're familiar with Laminas MVC, but we've approached this article on the assumption that more people will have prior experience of Twig.

The Mezzio Skeleton Installer does offer several alternatives for the templating engines:

  • Plates is a simple templating engine that sees the templates themselves as PHP files.
  • Twig is reliable, actively-developed and well-known in the PHP community.
  • laminas-view has a long history of being coupled to the Laminas MVC framework.

The default option for this step doesn't install any templating engine which is only relevant if you are building an API.

Submit 2 for Twig.

step-5-error-reporting

Submit 1 for Whoops. At this point, the installer has enough information to install the 3rd party packages in the application.

You can expect to see this prompt regarding dependency injection.

step-6-injection

Submit 1 for config/config.php.

step-7-injection-confirmation

Submit y for Remember this option for other packages of the same type? (Y/n).

The final step in the installation performs the following operations:

step-8-installation-complete

The result of your work so far

To run the application, you need to navigate to the installation folder using the command below.

cd ~/Projects/my-mezzio-project/

Then start the web server for your project.

php -S 0.0.0.0:8080 -t public

Now go to your favorite browser and type this URL http://localhost:8080/.

If everything worked correctly, you should now have a working application like in the screenshot below. You can then visit the recommended links to learn more about using the components we chose during the installation.

step-9-installation-complete

Coming up in the Mezzio101 series

With our base application set up, the next article will detail how to create new pages and modules to organize your content. For this purpose we will create new files, update the application configuration and also use the command line interface.

Additional Resources

Back to blog posts