Q:

Laravel Routing: How to retrieve a variable from a URL segment wrapped in curly braces?

If I have the following code:
Route::get('/gallery/{gallery?}', function(){
    echo $gallery; // how do I retrieve the gallery variable?
});

The reason I want to do this is because the structure of my site is changing and I want to redirect all requests to /gallery/{something} to /galleries/{something} with something like:
Route::get('/gallery/{gallery?}', function(){
    return Redirect::to('/galleries/{gallery}');
});

I would prefer to deal with all redirects within my routes file. How can I turn {gallery} into $gallery so I can do stuff with it?

A:

Just define the $gallery variable in your function() to capture it. Note - because you have marked {gallery?} as optional - you need to remember to give the function a default setting - i.e. null
Route::get('/gallery/{gallery?}', function($gallery = null){
    var_dump $gallery;
});

