Migrating a big ZF3 MVC application to Zend Expressive

I hope this question is not so vague as to annoy overly much. The answer may be “it depends” and/or “give us more information.” Nevertheless –

I’ve been working on a pretty big ZF3 MVC application using Doctrine. It has like 20 different controllers, 20+ entities classes, and so on. It’s basically CRUD, with authentication and authorization, and sending some emails around.

I am getting the sense that MVC is becoming passé, and Expressive is the thing, and I’d like this project to be future-oriented. I’ve barely played with Expressive (but to the extent that I have, I like it). Is it insane to consider migrating the whole thing? Do people do that?

I’ve read https://zendframework.github.io/zend-expressive/v3/why-expressive/, and the gist of that seems to be if you’re starting from scratch, go with Expressive. As to whether you might want to change directions even if you’ve gone as far down the MVC as I have, it doesn’t say (understandably).


In my opinion, the PSR-15 apporach ist far supperior to the old MVC approach.

The pipeline is pretty straightforward. And having the possibility to move several task to the general pipeline (f.e. authentication, authorization, …) or the pipeline of a specific route (f.e. validation, …) makes it easy to seperate matters. And there is a bunch of other advantages.

We’re currently (still) migrating a (super) huge application from ZF1 to ZE.

The steps we did are:

  • Setup a new expressive application
  • Move your old codebase to a seperate namespace, f.e. /src/Legacy/
  • Add a middleware (LegacyApplicationMiddleware) between the RouteMiddleware and NotFoundHandler, which bootstraps the legacy application. This will happen, if no “new” ZE route matched.
  • Add another middleware, which converts the ZF1 request/response to PSR-7 request/response classes. I think Zend already has a package for this, but we didn’t know that back then.
  • We also needed some other middlewares to convert between the old Zend_Session and a new session implementation and so on. I guess this depends on your project.

After that, we were able to write new features in the ZE part of the application.

The old codebase remains in the legacy folder and is bootstrapped and used, if no “new” route matches. If an “old” route matches, the legacy application will respond.
The requests/responses between the two worlds are converted via middleware.

Now we’re migrating controller after controller to new RequestHandlers/Middlewares/Services/etc. in Zend Expressive.

Thanks. So, it can definitely be done and is definitely worth the trouble, in your case. My case is kind of unusual. I work alone, and I don’t work fast. I am doing a full rewrite of my own creation, and have been working on this rewrite for a long time – embarrassing to admit how long, but I think when I began ZE was either not yet in existence, or very young. The point being, my project is not yet in production. I could keep going down the MVC road with a view towards migrating the whole thing after it’s “done,” so to speak, but at least I’d get it out there sooner. Or, undertake a major change of direction and take even longer. I know nobody can make up my mind for me, but it’s helpful to talk about it – kind of like psychotherapy. :slight_smile:

Based on the fact that you are not under any time crunch with your application, I would take to ZE route.
And yes, it depends on the structure of your current application :wink:
Let’s assume it is already in a nice layout as far as your existing controllers, services, models, VO’s and entities goes, and considering your own assessment of programming speed, it could easily be converted within 2-3 weeks.
In case you need some ''follow along" encouragement, take a look at Beachcasts from @AdamCulp . He is doing an awesome job with his videos.

1 Like

Hmmm, this starts to sound tempting, thanks. Now I will betray my naïveté by asking you what a VO is. :slightly_smiling_face:

1 Like

I’d like to give this a try, so I first want to make I sure I understand. I follow you well enough up until the part about the middleware before the 404 handler that “bootstraps the legacy application.” Not sure what that would look like. Do you have a code example, a pseudocode example, or a link to further information? My search engine efforts have not been productive. I’ve read https://docs.zendframework.com/zend-psr7bridge/ – if that’s the package I’d be using – and it’s a little sparse.

I would expect this ZE/MVC integration to be easier in my case than in yours, since my “legacy” code is ZF3 rather than ZF1, hence less incompatible.


Thanks for the tip about Beachcasts. That guy is good.

That’s why I recommended Adam’s videos for you :smiley:

This is about ZF1, but it should give you a good idea about what the code could look like: https://mateusztymek.pl/blog/upgrade-zend-framework-1-to-zend-expressive-psr-7-middleware

@PowerKiKi THANK YOU! From a quick read-through I can see this will help me immensely. I was on the cusp of figuring out that essentially you need to run the bootstrapping from the old index.php, but there are a few devilish details, and this article will help sort those, despite the significant differences between ZF1 and ZF3. I’m very new to this world of middleware, so it will be an even more thrilling adventure (learning cool new stuff is what it’s all about, so I’m in).

It will take some thinking to sort out which things are shared and which are separate, like config files, but that should not be overwhelming.

I was wondering, well, isn’t it expensive to bootstrap a whole application within an application? My answer to myself is, maybe, but in traditional web apps it’s totally common to do http redirects, which means “go back to the top and start over,” expensive as it may be. I would think this is a bit less overhead than that, and it’s meant to be temporary rather than forever, so why not.

If I do get this going maybe I should write a blog post. There does seem to be a dearth of documentation on this. Maybe someone among you ninja guys will give it a quick pre-publication review, since I am far less advanced… but I get ahead of myself.

Hey @steffenbrand, excellent explanation on the steps involved to migrate. :slightly_smiling_face: Would you mind sharing some code showing how you created middlewares for converting between Zend_Session and zend-expressive-session?

Thanks and regards,