Paul B
Paul B

Reputation: 349

Laravel Routing

In Laravel, I want to have two different routes that have the same URL, but that runs a different controller based upon the datatype of the input. For example:

Route::get('/name/{id}/', function($id)
{
    return 'id is an int:' . $id;
})->where('id', '[0-9]+');

Route::get('/name/{id}/', function($id)
{
    return 'id is a string: ' . $id;
})->where('id', '[a-z]+');

This doesn't seem to work, though - the second route seems to overwrite the first completely, so the app wouldn't support ids that were integers. How do you actually accomplish this in Laravel without doing the checking manually inside the route?

Thanks

Upvotes: 0

Views: 261

Answers (2)

Razor
Razor

Reputation: 9835

To not overwrite the first route, use different parameter name

Route::get('/name/{id}/', function($id)
{
    return 'id is an int:' . $id;
})->where('id', '[0-9]+');

Route::get('/name/{stringId}/', function($id)
{
    return 'id is a string: ' . $id;
})->where('stringId', '[a-z]+');

Upvotes: 2

Chrissx
Chrissx

Reputation: 357

I think you can seperate this two routing mechanish from each other.

 Route::get('user/{id}', function($id)
{
    //
})
->where('id', '[A-Za-z]+');

Route::get('user/{id}', function($id)
{
})
->where('id', '[0-9]+');

This code sample from Laravel site. If you want seperate logic more than that you can use filter. Filter sample:

Route::filter('foo', function()
{
    if (Route::input('id') == 1)
    {
        //
    }
});

I hope i can help you.

Upvotes: 0

Related Questions