Laravel Routing Conflict issue

2019-03-04 13:28发布

问题:

I have following url in laravel:

1.Need to change From:

localhost/laravel/page/2/

to

localhost/laravel/2/

My Route.php is

Route::get('page/{id}/',
          array(
        'as'   => 'page', 'uses' =>'Frontcontroller@page'));

But When I change to this,

Route::get('/{id}/',
          array(
        'as'   => 'page', 'uses' =>'Frontcontroller@page'));

I have noticed that it has conflict issues with other route ,Plz help me

Thanx in advance

回答1:

Just declare the new routes at the last of your all other routes and also add a where clause, for example, try something like this:

//All other routes ...

Route::get(
    '/{id}',
    ['as' => 'page', 'uses' =>'Frontcontroller@page']
);

Optionally you may add a where clause like this:

Route::get(
    '/{id}',
    ['as' => 'page', 'uses' =>'Frontcontroller@page']
)
->where('id', '[0-9]+'); // for id as integer