thanks alot
[Package] LumenRouteBinding : Adds support for "Route Model Binding" in Lumen
Hi there!
Recently I started using Lumen, I loved it as it was small and fast and perfect for small projects (I used previous versions which had support for views and sessions) and It was perfect except just for one thing..
I was surprised that it didn't support Route Model Binding, I searched but I didn't find any package that adds this functionality. Then I figured out that it's because Lumen doesn't use the Illuminate router, Instead, It uses the FastRoute which is faster but doesn't allow for route model binding.
So I created this Package which extends the FastRoute to add support for Route Model Binding to Lumen.
It requires :
php >= 5.4.0
Lumen 5.*
It supports Explicit binding :
$binder->bind('user', 'App\User');
And Implicit binding :
$binder->implicitBind('App\Models');
And Composite binding : (bind more than one wildcard together), In situations like :
posts{post}\comments{comment}
you will be able to bind it to a callable that will resolve the Post and the Comment related to the post)
$binder->compositeBind(['post', 'comment'], function($postKey, $commentKey) {
$post = \App\Post::findOrFail($postKey);
$comment = $post->comments()->findOrFail($commentKey);
return [$post, $comment];
});
Also can work with other classes like Repositories :
// Add a suffix to the class name
$binder->implicitBind('App\Repositories', '', 'Repository');
Can use a custom method on the repository :
// Use a custom method on the class
$binder->implicitBind('App\Repositories', '', 'Repository', 'findForRoute');
Where in the repository class you can do :
public function findForRoute($val)
{
return $this->model->where('slug', $val)->firstOrFail();
}
Check it out and let me know if you have any questions, I hope it helps someone :)
Please or to participate in this conversation.