'Middleware and authentication with laminas-mvc

I had an old ZF (actually ZF1) application laying around, so i decided to play a little bit with laminas/mezzio to see how it works and if updating would be possible. So as it is an old ZF1 application, laminas-mvc seems the way the go, so i started with https://github.com/laminas/laminas-mvc-skeleton. I got it up and running very quick, but soon when i started to extend it i hit a wall. I think the documentation provides the technical guides but i am missing some conceptual insights and how to connect different parts.

The first thing i actually wanted to implement is authentication. Just a POC. I figured middleware is the place to look first but i got stuck pretty soon. With middleware in general. Looking at the docs of laminas-mvc only point to laminas-mvc-middleware which provides a recipe to replace the controller dispatching with defined middleware. But why should i use MVC and then replace the controller, i want to use middleware on top of that, otherwise i could just use mezzio. Speaking of mezzio, its documentation does not help either as the middleware instantiation and handling seems to be done in significant different manner than in laminas-mvc.

I also looked at laminas-authentication in aspiration it would provide a drop in solution to my problem. But it also lacked the part on how to integrate with a laminas-mvc application.

So in essence my questions are:

  1. How would you integrate middleware without losing the controller dispatching mechanism?
  2. If i would better off without using middleware, how would you then setup something like laminas-authentication?
  3. If 2 is true, how would one use already present solutions like mezzios authentication middleware?


Solution 1:[1]

I would suggest. You need to make a decision early on if you are going to use Mezzio or the MVC. If you are going to use the MVC use controllers, not middleware in place of controllers. If you want to use Middleware, go Mezzio, if you want controllers, go MVC. Are they about the same thing? Sorta. In the MVC an instance of AuthenticationService is already registered with the servicemanager if you use the component installer to install laminas/authentication. Please see this doc for reference:

https://docs.laminas.dev/laminas-component-installer/

In short. And this is coming from someone that also had a zf1 application that I wanted to port to Laminas. They, as in the framework devs such as MWOP, recommend going to mezzio. For me, that was to steep a learning curve since I stopped developing before zf2 and just recently returned.

  1. If your coming from zf1, using controllers is your path of lease resistance since there a very shallow learning curve.
  2. Nearly all of the "services" you will need is already registered with the service manager out of the box with the MVC as long as you use the component installer.
  3. The hardest thing for me coming from zf1 was the "concept" of "services" the way that they are now implemented in Laminas. All I can really tell you is, stick with it, its worth it. I think I am currently seeing .45 second page loads in a custom CMS.

Things to consider. Since you are familiar with zf1 then the MVC will make sense... Ive still not got my head around mezzio in full. They say its better, and from what Ive read, the performance is there, blazing fast. However, for me. Something familiar was important. Also, coming from zf1. If I can suggest anything that will help you... Read the docs for the service manager! And if you have any question in regards to either mezzio or the MVC the maintainers will answer @ https://docs.laminas.dev/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyrsson