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:
- How would you integrate middleware without losing the controller dispatching mechanism?
- If i would better off without using middleware, how would you then setup something like
laminas-authentication
? - If 2 is true, how would one use already present solutions like mezzios authentication middleware?
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.
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/
Edit to add: You must bare in mind that unless you select the laminas-di component during installation or afterwards a factory for Laminas-Authentication will not be "autowired" for you. The laminas-authentication does not provide an out of the box factory. It only sets up the alias for the AuthenticationService/AuthenticationServiceInterface. You still must provide an factory for the service if you are not using laminas-di. I do not suggest using laminas-di. It creates more problems than it solves for people new to the framework.
Also, Middleware really only exist in the MVC to ease migration to Mezzio. It is not recommended to use them in place of Controllers in the MVC.