Reputation: 8075
cant find anything on this but im sure its simple.
I have a route that I need to duplicate 4 times as I use the url to change the query.
Currently:
Route::get('/', function()
{
$builds = Blog::findBuilds();
return View::make('pages/home', compact('builds'));
});
What I want to do is e.g.:
Route::get(array('/', '/trending', 'staff-picks'), function()
{
$builds = Blog::findBuilds();
return View::make('pages/home', compact('builds'));
});
But that of course does not work. Whats the trick?
Upvotes: 3
Views: 5366
Reputation: 152860
As Laravelian's answer demonstrates you can and should use a dynamic route parameter. However if you just do
Route::get('{slug?}', function($slug = null){}
you will catch every request (except the ones that matches a route that was defined before this one)
To have more control use a regular expression:
Route::get('{slug?}', function($slug = 'index')
{
$builds = Blog::findBuilds();
return View::make('pages/home', compact('builds'));
})->where('slug', '(trending|staff-picks)');
Now slug
has to be either: nothing (because its an optional parameter {...?}
) trending
or staff-picks
Upvotes: 6
Reputation: 598
Route::get('/{slug}', function($slug = null) {
$builds = Blog::findBuilds($slug);
return View::make('pages/home', compact('builds'));
});
You're better off though, putting the function in a controller, and passing it this way:
Route::get('/{slug}', 'BlogController@getSlug');
Route::get('/', 'BlogController@getSlug');
Upvotes: 0