mardi 22 janvier 2019

Laravel - Strange happenings on routes

I have searched online but nothing seems to match my issue. I have a little idea of what it may be related too, but not sure. I have a basic User CRUD. For my routes, I am adding them in web.app but trying to prefix them with api/

When I list my routes, I can see the correct routes, here is an example of the index

| GET|HEAD  | api/user | user.index | App\Http\Controllers\UserController@index | web,auth

So within web.php I have a basic resource

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

Now when I hit this route, it doesnt seem to work. So I thought it may be to do with the fact that the api.php file has an api prefix. So within this file, I added

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

And now it works. This is where it is weird though. If I remove the initial resource from web.php, I get an authentication error. To have it work, I need the resource within web.php and the get in api.php.

However, I can make POST calls directly to the User Controller directly within web.php

Route::post('api/user', 'UserController@store');

This works without the need to add anything within api.php. This is where it gets weird though. I thought it must be the api prefix, although this does not really explain why I need it in both route files. So within web.php, I changed it to

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

But once again, without anything within api.php, the GET does not work but the POST does.

Does anyone have any idea of what is going on?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire