Sahil
Sahil

Reputation: 739

Routing in laravel framework

I using the laravel framework of php for development.I done these following steps

  1. I define Route::resource('users', 'UsersController'); in route file and then define Route::get('user/pingme', 'UserController@pingme');

  2. When i make a get call to pingme function, it was not working .I was getting the response code is 200 but code inside that pingme function was not working and i do not know why.

  3. then i changed it to Route::post('user/pingme', 'UserController@pingme'); it was working fine as needed.

  4. then what i did is, removed Route::resource('users', 'UsersController'); and make again get route to ping me function and make get call and it starts working fine .

so this is any bug in framework(rare thing) or i am missing something(probably yes)? Help me out....

Upvotes: 1

Views: 118

Answers (2)

Monika Yadav
Monika Yadav

Reputation: 381

Route file works as follows:-

  1. if you have wrote a mapping for controller only, then it needs to come at the bottom of all other route mapping otherwise your program controller will pick route from user controller only and will redirect to UserController. so the right order of all routes is:-

    Route::get('user/pingme', 'UserController@pingme');

    Route::post('user/logout', 'UserController@logout')->before('auth');

    Route::resource('user', 'UserController');

OR

 Route::post('user/logout', 'UserController@logout')->before('auth');

 Route::get('user/pingme', 'UserController@pingme');

 Route::resource('user', 'UserController');

Upvotes: 1

SUB0DH
SUB0DH

Reputation: 5240

In your route file, the order of the routes needs to be as follows:

Route::get('user/pingme', 'UserController@pingme');

Route::post('user/logout', 'UserController@logout')->before('auth');

Route::resource('user', 'UserController');

If Route::resource('user', 'UserController') comes before the other routes, the GET request to user/pingme will be handled by show method inside of UserController, because it is how Resourceful Controllers work. So, the Route::resource for user needs to come after all other routes with user/ prefix.

Upvotes: 0

Related Questions