2

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?
patman
  • 2,780
  • 4
  • 30
  • 54
  • First, note that I never worked with Laminas and the following is my personal opinion based on my understanding of the Laminas docs. **To (1)**: The doc page [Dispatching PSR-7 Middleware](https://docs.laminas.dev/laminas-mvc/middleware/) and the docs of [laminas-mvc-middleware](https://docs.laminas.dev/laminas-mvc-middleware/) component describe in detail, how you can integrate middlewares into your laminas-mvc projects. – PajuranCodes May 06 '21 at 17:23
  • In principle, it seems that you should install the component, create your middleware class (or use an external one) and then define a route with the following key/value pair in the `defaults` item: `'controller' => PipeSpec::class, 'middleware' => ::class`. – PajuranCodes May 06 '21 at 17:23
  • **To (2)**: Imho, you'd be better off using [middleware for authentication](https://docs.mezzio.dev/mezzio-authentication/). See also [laminas-authentication adapter for mezzio-authentication](https://docs.mezzio.dev/mezzio-authentication-laminasauthentication/). Anyway, if you decide not to use a middleware: it seems that the component [laminas-authentication](https://docs.laminas.dev/laminas-authentication/) is defined as a service. So, it should be injectable into controllers, without any problems. – PajuranCodes May 06 '21 at 17:23
  • **To (3)**: You probably wanted to mean _"If 2 is false, ..."_. Then, you would integrate [middleware for authentication](https://docs.mezzio.dev/mezzio-authentication/) as described in my first comment. Good luck. _P.S:_ I found your question interesting. – PajuranCodes May 06 '21 at 17:23
  • By the way, maybe you'll find this article interesting: [Should You Choose mezzio Over laminas-mvc?](https://docs.mezzio.dev/mezzio/v3/why-mezzio/). – PajuranCodes May 06 '21 at 17:41
  • Hi @dakis, thanks for your responses. Firstly, yes at least for the moment i went with mezzio. There it is clear to me and i implemented the middleware with no problems. Your comments provided a little bit more insight and maybe i confused some concepts here. My problem was (and is still): i want to keep the controller mechanism of laminas mvc, which in my opinion gets overwritten once you define custom middleware for a route. And i wanted somehow to combine both, keep the laminas-mvc mechanism while adding my own middleware to it. – patman May 07 '21 at 07:30
  • You are welcome. In [Should You Choose mezzio Over laminas-mvc?](https://docs.mezzio.dev/mezzio/v3/why-mezzio/) you read some arguments for choosing _mezzio_ over _laminas-mvc_, right? Now, why exactly do you want to keep the controller mechanism provided by laminas-mvc, and therefore the whole rules enforced by laminas-mvc? If I was you I would also put the following in balance: – PajuranCodes May 07 '21 at 08:20
  • In my opinion, in a correctly implemented MVC-based application, a controller action has the sole responsibility of delegating the handling of the request to a certain [application service](https://www.martinfowler.com/eaaCatalog/serviceLayer.html) - therefore containing only 1-3 lines of code - and receives the instance of the request as dependency, maybe along with the route parameters - which must be defined as optional. – PajuranCodes May 07 '21 at 08:21
  • In comparison, in _mezzio_, a middleware class must implement the method _process()_, which receives the instance of the request as dependency, from which the route parameters [can be retrieved easily](https://docs.mezzio.dev/mezzio/v3/features/router/intro/#retrieving-matched-parameters). _P.S:_ I didn't mention the _view_ component on purpose. I think, this part is one that should be chosen by yourself, because there can be more options for it. – PajuranCodes May 07 '21 at 08:40
  • For example, as I see it for now, in _mezzio_, you could handle the presentation of the results of the user request (e.g. the returning of a `ResponseInterface`) directly inside the method _process()_ of the middleware referenced by the route. Or you could define a separate, additional middleware (with the role of a _view_ class) to handle only the presentation of the results of the user request (e.g. the returning of a `ResponseInterface`). – PajuranCodes May 07 '21 at 08:42
  • You are right in almost everything. Controllers are in fact slim and i will probably stay with mezzio for now (i currently implemented this in mezzio, where i have no problems with all that). I still wanted to try laminas (for module and event system mainly) and it seems to me like a very basic question to answer before starting to try laminas :D So again all your points are valid, maybe even the right answer but i am also interested in a recipe for laminas-mvc. – patman May 07 '21 at 09:28
  • Indeed, _"I still wanted to try laminas (for module and event system mainly)"_ is a very good reason to start using _laminas-mvc_. And I hope you'll find the way to easily combine it with middleware functionality ;-) – PajuranCodes May 07 '21 at 09:58

1 Answers1

0

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/

Tyrsson
  • 1
  • 1
  • 3