Alexander Solonik
Alexander Solonik

Reputation: 10260

get route in laravel overriding resource route , How to overcome this

I have the following route in my laravel route file:

Route::get('/{id}' , [
    'uses' => 'PagesController@show',
    'as' => 'getArticle'
]);

The problem with the above route is , its overriding the below route:

Route::resource('admin', 'adminController');

I want to keep my resource route, but how do i keep my resource ? is there a way around this ??

Upvotes: 2

Views: 435

Answers (3)

Leonid Shumakov
Leonid Shumakov

Reputation: 1319

There are two options:

  • move Route::get('/{id}', ...) after Route::resource(...)
  • or add a pattern to Route::get() if id is numeric Route::get('/{id}', ...)->where('id', '[0-9]+');

Upvotes: 1

Gayan
Gayan

Reputation: 3704

Modify your route file like this.

Route::resource('admin', 'adminController');

Route::get('/{id}' , [ 'uses' => 'PagesController@show', 'as' => 'getArticle' ]);

Route files executed in the order they are defined.

If you define Route::get('/{id}',.... in the beginning and set your url like http://your-site/admin, the admin section will be considers as the id for the Route::get('/{id}',.... route. So you need to keep it in your mind when you define your route.

Upvotes: 3

sandeep goel
sandeep goel

Reputation: 104

just move this route in the end of the web.php file.

Route::get('/{id}' , [
'uses' => 'PagesController@show',
'as' => 'getArticle'
]);

Upvotes: 1

Related Questions