Jun 26, 2016
0
Level 5
Lumen reusing Middleware queries
For the sake of the example, let's say I have a blog with "Posts" that can be private or public. If a user tries to go to a post that is private, they get redirected back to the homepage. So in my Middleware I'd have something along these lines:
$post = Post::find(2);
if ($post->is_private) {
return redirect('/');
}
return $next($request);
This works fine, but the bad part is if the Middleware passes, once I get to my Controller I have to do ANOTHER query to the database again to get the Post, even though we already got it in the Middleware. In Laravel this is solved by Route Model Binding, but I can't find a good solution in Lumen apart from just copying the logic. Has anyone found a better solution?
Please or to participate in this conversation.