What do you think about middleware approach?

Middlewares approach is used in Java web apps for years. After being adopted well in JavaScript server frameworks recently and acceptance of PSR-7 it starts its raise in PHP community.

https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html

What do you think about it?

1 Like

Are there any good resources/articles/blog posts about this?

It sounds … interesting

If I got it right, Middleware is some closure-like thing that you can plug in to manipulate request and response objects.

Here is how it is implemented in Slim:

http://www.slimframework.com/docs/concepts/middleware.html

Yes, you got it right. It’s a bare thing which is getting request and response references and could do something with these. $next thing for passing processing to other middlewares. It could be read as $allOtherMiddlewaresDoSomethingWith($request, $response).

1 Like

This would mean that the people who prefer the Laravel approach (Slim, Express.js, etc.) to routing could just plug in their favorite routing middleware and have the cake while eating it :)

Provided that the middleware follows the PSR-7.

That sounds nice. ;)

Well, Laravel approach is alike but isn’t compatible with everything else. Slim and Expressive are.

Hey guys, just to share some info: Laravel is not MVC framework anymore.

Middleware approach is interesting, I used it in a Node.js app, but need to analyze its true benefits.

It still is if we’re talking about what’s called MVC by most of the web frameworks.

Yes. I meant: The don’t consider “MVC” guys anymore.

If you are instered, I found this beautiful framework that uses this approach:

Feathers

Have a nice day.

PD: Yii is so beautiful :)

I’ve been using middlewares (mostly for APIs), there are cons and pros, but overall, our team love the middleware approach. It makes development more simple and transparent

I think it would be cool to see a middleware yii extension as an alternative routing system

Hi Alex I would love to check it but the website is not working. Did you deleted it or you have some hosting problems atm? Cheers

Works for me.

Are middlewares really that important? I mean we can use Yii2 Core Filters and beforeAction() to achieve anything that needs to run before an action. I find that to be much simpler than using a different class to control a certain controller for example.

Are there any important use cases for middlewares that are currently holding us back from achieving the same goal in Yii2?

Not really. Filters are, in fact middlewares. Just Yii-specific ones. It makes sense to introduce PSR middlewares because we’ll be able to use existing third party code.

So using middleware, for instance feeding something to the View object before the controller is loaded, such as navigation from the DB, can that be implemented using Yii now or would a new application layer need to be created first? If it can be, what would be an an example of it?

It’s not built into current Yii.

Do you think it’s something that should or will be added? It seems like it would be useful in quite a few ways.