0

I have just created a Laravel project and testing the first endpoint through Insomnia, however, this always returns 404.

In the project directory, I have installed valet and used its URL, which then returns 404 whenever I run a request through Insomnia.

Also when I run valet open from the project directory this opens a web page with the Laravel main page which makes me think that valet is working fine.

the URL in the URL field is the one I am using on Insomnia

api.php

Route::group([
  'middleware' => 'api',
  'prefix' => 'auth'
], function ($router) {
  Route::post('register', 'AuthController@registerUser');
  Route::get('', 'AuthController@testing');
});

AuthController.php

public function registerUser()
{
  return response()->json('User signed up');
}

public function testing()
{
  return response()->json('Test ok');
}

The snipped code above is the route that I currently have in place which should simply go to the AuthController file and run the registerUser method.

Whatever I am doing on Insomnia is:

enter image description here

heyr
  • 5,514
  • 11
  • 34
  • 55
  • 1
    what other end points have you tried? can you show what you are doing in that controller method .. where did you register this route? – lagbox Nov 16 '19 at 21:18
  • @lagbox In the functions, I am just returning a message just to make sure that at least the request goes through, and btw I have also updated the question. – heyr Nov 16 '19 at 21:26
  • you are saying all end points return 404, what other end points did you try? and where did you define this route you have above? – lagbox Nov 16 '19 at 21:34
  • So the first one is /auth/register POST request, and the second one is /auth GET request, both returning 404, they are both defined in the api.php file. Please see at the post updated, all the info are there – heyr Nov 16 '19 at 21:36
  • missing `'` on AuthController.php – Alberto Sinigaglia Nov 16 '19 at 21:40

1 Answers1

2

Your routes are probably not for the URLs you think they are. They are probably:

mentorme-api.test/api/auth
mentorme-api.test/api/auth/register

The routes/api.php file gets loaded in a route group with prefix api for you.

Run php artisan route:list from the command line to see how your routes have been registered.

The group definition the routes/api.php routes get loaded into:

Route::prefix('api')
     ->middleware('api')
     ->namespace($this->namespace)
     ->group(base_path('routes/api.php'));

Prefixed with api and already have the api middleware applied. You can remove 'middleware' => 'api' from your group definition for your 2 routes.

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • You're right, by including api in the url this goes through, God, I went mental for a while! Thank you buddy – heyr Nov 16 '19 at 22:36
  • 1
    no problem ... how these route files get registered is in `app/Providers/RouteServiceProvider.php` btw – lagbox Nov 16 '19 at 22:37
  • Yeah, I just made changes in the file, and is all good! Thank you again! – heyr Nov 16 '19 at 22:39