D
D
damn2019-08-21 00:52:57
Laravel
damn, 2019-08-21 00:52:57

Middleware or Controller?

What is the point of using intermediaries if you need to check if Age is valid, if less than 21 then redirect to the main page, if you can do the same with a controller? The documentation gives exactly an example with intermediaries, but I did not catch the meaning.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2019-08-21
@lovebarcafc

lovebarcafc ,

but damn it, creating an intermediary, declaring it and then connecting it, is not entirely appropriate for passing one simple parameter

What if there are more options? And there are more than 1 controllers in which this is done too? The point of middleware is that it can be applied massively from one place - to all routes, only api, console or web, a group of routes ... For example, I am writing an API for an iOS application, and I want to be sure that all requests for my routes contain a Content-Type: application/json header. Sawing under this middleware is the very thing.
Middleware should not be seen as an example in the dock with age, but as layers through which request and response pass. Request enters the application and passes through a series of "filters" (middleware), then the same thing happens with the response.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question