Yii Framework Forum

What do you think about middleware approach?


(Alexander Makarov) #1

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?


(Jacob Moen) #2

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

It sounds … interesting


(Jacob Moen) #3

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


(Alexander Makarov) #4

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).


(Jacob Moen) #5

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. ;)


(Alexander Makarov) #6

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


(Alexander Makarov) #7

(Zorobabel Omicron) #8

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.


(Alexander Makarov) #9

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


(Zorobabel Omicron) #10

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


(Zorobabel Omicron) #11

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

Feathers

Have a nice day.

PD: Yii is so beautiful :)


(Ducntq) #12

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


(A Dickerson) #13

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


(Evanaftelm) #14

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


(Alexander Makarov) #15

Works for me.


(Frixou89) #16

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?


(Alexander Makarov) #17

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.


(Knight Yoshi) #18

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?


(Alexander Makarov) #19

It’s not built into current Yii.


(Knight Yoshi) #20

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