Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created post Mezzio101 #250

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bootstrap/scss/_custom-styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ pre[class*=language-] {
font-size: smaller;
}

article {
img {
max-width: 100%;
}
}

.dropdown-item {
&.active,
&:active {
Expand Down
173 changes: 173 additions & 0 deletions data/blog/2025/2025-01-30-mezzio101-using-mezzio-skeleton-installer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
---
id: 2025-01-30-mezzio101-using-mezzio-skeleton-installer
author: bidi
title: 'Mezzio 101: Create an Application Using the Mezzio Skeleton Installer'
draft: false
public: true
created: '2025-01-30T11:00:00-01:00'
updated: '2025-01-30T11:00:00-01:00'
tags:
- mezzio
- skeleton
- tutorial
---

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.

<!--- EXTENDED -->

> 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](https://www.php-fig.org/).

#### Prerequisites

You need a working installation of [PHP](https://www.php.net/manual/en/install.php) and [Composer](https://getcomposer.org/) available on your `$PATH`.

### What is Mezzio?

As mentioned on [the Zend official site](https://www.zend.com/resources/what-mezzio-why-would-i-use-it), '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.

```shell
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is meant by this? Using components of Laminas?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is meant by this? Using components of Laminas?

It means the default options in the installer: laminas-servicemanager instead of the rest of DI libraries, fastroute , and twig

Copy link
Member

@froschdesign froschdesign Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default option for template engine is n (none) which raises the question of why Twig is chosen. (The tutorial does not explain this.)

Copy link
Member

@arhimede arhimede Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default option for template engine is n (none) which raises the question of why Twig is chosen. (The tutorial does not explain this.)

I am in doubt about that.
Indeed none is the default option, but we should consider using Twig in this series of tutorials in order to provide some practical value for the developers.
Or we should have one article about all optional templates which can be used.
I really do not think that somebody use no template engine in a real life application. Eventually only if they need only CLI I/O

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expanded the section for choosing the templating engine.

Funnily enough, the installation should complete in less time than it takes for you to read this tutorial.

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

You should see the text and prompt below:

![step-1-installation-type](/images/blog/mezzio101/mezzio101-use-skeleton-01.jpg "step-1")

Submit `3` for option `Modular`.

![step-2-dependency-injection](/images/blog/mezzio101/mezzio101-use-skeleton-02.jpg "step-2")

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](/images/blog/mezzio101/mezzio101-use-skeleton-03.jpg "step-3")

Submit `1` for `fastroute`.

![step-4-templating](/images/blog/mezzio101/mezzio101-use-skeleton-04.jpg "step-4")

We chose `Twig` as the templating engine because of its concise syntax, extensibility and well-written documentation.
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` is related to Laminas MVC.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you've chosen twig - but perhaps extrapolate on the reasons not to eat our own dog-food and use laminas-view. For example: "… 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"

laminas-view works perfectly well with with Mezzio, even though it does have a long history of being coupled to the MVC framework.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you've chosen twig - but perhaps extrapolate on the reasons not to eat our own dog-food and use laminas-view. For example: "… 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"

laminas-view works perfectly well with with Mezzio, even though it does have a long history of being coupled to the MVC framework.

we have in plan an article dedicated to all template engines options in mezzio.
Definitely i think it is a subjective choice between them


The default option for this step doesn't install any templating engine, but we only recommend this option if you want to build a CLI or API.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might use Laminas components for CLI applications, but Mezzio, not so much :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might use Laminas components for CLI applications, but Mezzio, not so much :)

Well, it's the idea that you do not need a template engine for CLI.
The article is about Mezzio :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, why would you use the Mezzio skeleton to scaffold out a CLI app? The CLI bit needs removing so it's just:

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](/images/blog/mezzio101/mezzio101-use-skeleton-05.jpg "step-5")

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](/images/blog/mezzio101/mezzio101-use-skeleton-06.jpg "step-6")

Submit `1` for `config/config.php`.

![step-7-injection-confirmation](/images/blog/mezzio101/mezzio101-use-skeleton-07.jpg "step-7")

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:

- Sets up `phpcs` the [PHP Code Sniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer)
- Clears the cache
- Enables development mode

![step-8-installation-complete](/images/blog/mezzio101/mezzio101-use-skeleton-08.jpg "step-8")

#### The result of your work so far

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

```shell
cd ~/Projects/my-mezzio-project/
```

Then start the web server for your project.

```shell
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](/images/blog/mezzio101/mezzio101-use-skeleton-09.jpg "step-9")

#### 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

- Learn more about [Laminas](https://docs.laminas.dev/).
- Learn more about [Mezzio](https://docs.mezzio.dev/) and its [features](https://docs.mezzio.dev/mezzio/v3/getting-started/features/).
- Learn more about [Mezzio's fundamentals from Matthew Setter's book 'Mezzio Essentials'](https://mezzioessentials.com/).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.