Reputation: 2468
I am experimenting with Laravel 5 and came across HTTP Middleware. I am curious to know whether it is advisable to access database (checking) from middleware and filter HTTP requests as per the database outcome?
P.S I was able to perform database queries in middleware.
Upvotes: 7
Views: 2652
Reputation: 152890
I don't see why accessing the database in middleware would be bad practice. Take a permission system for example. Your middleware would have to verify that the logged in user is allowed to view the current page. There's no way to do that without querying the database (except if you'd get the permissions from somewhere else)
If this query probably runs on many requests you should make sure that you optimize it properly and reduce the query time to a minimum.
Upvotes: 12