Laminas Framework Series: Middleware in Mezzio and MVC

Doğan Uçar
4 min readSep 26, 2023

The second post of “Laminas Framework Series” compares Mezzio and the framework based on “Model-View-Controller” (short: MVC). The Laminas Framework is for its own is relatively new, but since it is an entire fork and a continuation of the Zend Framework, it has very deep roots in the enterprise software development with PHP. Mezzio — formerly known as Expressive — has the middleware approach in its heart.

The first blog post addressed “Background and Basics”. While each post is independent and does not require the knowledge from it successor(s), it is highly recommended to read them first to have a good overview to Laminas.

Middleware Flow
Middleware Flow

Core Concepts

The Zend Framework has always been characterized by MVC. With the evolving demand of PHP in web applications, companies and developers were in the demand of robust, scalable frameworks to work with. Inspired by other industry standard frameworks and programming languages, MVC began its journey, grow over time and has proven it. MVC is feature rich, modular and reusable.

On the other hand, MVC is also very complex and a lot of things are configured into the framework. In the era of microframeworks and API based solutions, the demand for liteweight, modular and standardized frameworks increased more and more. Mezzio is the answer to this demand.

Middleware Architecture

In contrast to MVC, Mezzio is based on the middleware architecture. While MVC, revolves around controllers and actions, where routes lead to controller actions which form the bridge between logic and presentation, Mezzio is utilizes the middleware architecture to pass requests from one layer to the next. Each middleware has a specific task: to process a request, modify it, generate a response, or hand over to the next middleware. This pipeline of request-response handling offers unparalleled flexibility.

This design offers two main advantages:

  1. it provides a clear and consistent flow for handling requests, allowing developers to pinpoint and modify specific stages of the request lifecycle.
  2. it promotes the decoupling of logic, enabling functionalities like authentication, logging, or caching to be wrapped up in individual middleware components and plugged in as required.

It is important to outline the difference here: while Mezzio’s middleware architecture is like an onion, where the request is passed from the most outer layer to the inner layers, MVC requests target a single controller.

Middleware Templating with Ease

Early web developer will remember: there is a business part and there is a view part, usually HTML generated by templates. Many template engines for PHP exist and each project has its own reasons to use a specific engine.
Mezzio solves this problem by simple abstraction: While it does not tie developers to a specific templating engine, it certainly doesn’t leave them stranded. Mezzio offers an integration point for popular templating engines like Twig or Plates. This approach embodies Mezzio’s philosophy: provide the tools, but let developers choose their path.

In contrast to this, Laminas MVC integrates laminas-view as the single option for the template engine, a robust system that offers view helpers, layouts, and more, encapsulating the richness of its legacy.

Depedency Injection: The Key for Scaling Applications

Dependency injection and service management are the key ingredient for enterprise applications. Managing dependencies as part of the application does not only reduce boilerplate code, it also leads to cleaner and robust code that makes development easy.

Mezzio gracefully accommodates this standardized container compatibility. Instead of dictating a specific container solution, Mezzio ensures that as long as the chosen solution adheres to the PSR-11 standard, it can be seamlessly integrated.

Both, Mezzio and MVC lean heavily on this concept. Personally, I would say this is the most critical thing about Laminas at all that makes its enterprise story a success.

Performance and Optimization in Middleware

Performance is not just about speed; it is an art of optimization. Mezzio, with its lightweight nature, often delivers swifter responses. Yet, with the right middleware choices, its performance can further soar. Laminas MVC, being feature-dense, might require a more hands-on approach for optimization, employing techniques from opcode caching to event listener tuning.

Conclusion

You will probably miss now the part with “what is better to use?”. The answer is very easy: it depends. While MVC has proven its ability for large enterprise applications, Mezzio is good for microservice development or prototyping. It is up to you, your project needs and the team what suits better.

As we close this chapter, it’s essential to recognize that this blog is but a fragment of a much grander tapestry: an upcoming book dedicated entirely to Laminas Mezzio. If this snippet has piqued your interest, ensure you don’t miss out on the larger narrative by registering for our newsletter. We’re excited to share more insights, tales from the trenches, and expert advice on Laminas Mezzio. Thank you for joining us on this journey, and we eagerly await our next interaction.

About Me

My name is Dogan, I am a senior software engineer with 10+ years of experience of software engineering in different domains and provide my professional services as a freelancer. I shared parts of my former experience with Laminas here. If you need help with your PHP and/or Laminas projects, do not hesitate to get in touch with me. I am looking forward! 🙂

Originally posted on: https://www.dogan-ucar.de/laminas-framework-series-middleware-in-mezzio-and-mvc/

--

--

Doğan Uçar

Software Engineer, PHP/Laminas (Zend), Backend, Cloud, Freelancer & CEO, Open Source Contributor