In my app I have something like this:
$router->pattern('book', '[^/]+/[^/]+');
$router->bind('book', function ($bookString) {
list($authorNameSlug, $titleSlug) = explode('/', $bookString);
$author = Author::whereNameSlug($authorNameSlug)->firstOrFail();
$book = $author->books()->whereTitleSlug($titleSlug)->firstOrFail();
return $book;
});
I am referring to books as author/title in my URLs from many places. Then, I can do something like this:
Route::get('{book}/reviews', 'BookReviews@index');
Route::get('{book}/buy', 'BuyBook@form');
And have URLs like:
- book.dev/j-k-rowling/harry-potter-and-the-philosophers-stone/reviews
- book.dev/antoine-de-saint-exupery/the-little-prince/buy
All I need to do is to put {book} in the route. This is Laravel 5 though, not sure if it works for Laravel 4.