Question

I recently migrated an application from Laravel 3 to Laravel 4 and came across something that was used in Laravel 3 that I couldn't find an equivalent to in Laravel 4.

In Laravel 3 there was a Route::share and in my application there was the following shared route that basically redirected a series legacy URL's to a new url. Obviously, this could be done with mod_rewrite rules, but there might be other behavior besides simple redirects that happen in the route that could be shared.

Route::share( array(
    array('GET', 'Songbook/songbook.html'),
    array('GET', 'Songbook'),
    array('GET', 'songbook/songbook.html'),
    array('GET', 'songbook'),
    array('GET', 'tgbs'),
), function () {
    return Redirect::to( 'lyrics', 301 );
} );

Is there an equivalent to this in Laravel 4? The only thing I was able to do was to create discreet routes for each of them. It seems like there should be a more elegant way:

Route::get('Songbook/songbook.html', function(){ return Redirect::route('lyrics'); });
Route::get('Songbook',               function(){ return Redirect::route('lyrics'); });
Route::get('songbook/songbook.html', function(){ return Redirect::route('lyrics'); });
Route::get('songbook',               function(){ return Redirect::route('lyrics'); });
Route::get('tgbs',                   function(){ return Redirect::route('lyrics'); });
Was it helpful?

Solution

I'm going to post this as an answer to my own question because I found something that works. This uses a url parameter and the where() method with a regex to determine if it matches any of the urls in a list. However I still don't feel that this is as elegant as what was in Laravel 3 (unless you really like regular expressions and find them elegant).

I still feel there is a more elegant solution.

Route::any('{any?}', function(){ return Redirect::route('lyrics'); })
    ->where('any', '(Songbook\/songbook\.html|Songbook|songbook\/songbook\.html|songbook|tgbs)');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top