2

I try to use API routing but my routes don't work.

When I try to login with http://xxxxxxx/api/v1/auth/login

The server sends me a 404 error. Do you have an idea?

// Auth Endpoints
Route::group([
'middleware' => 'cors',
'prefix' => 'v1/auth'
], function ($router) {
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LogoutController@logout');
Route::post('register', 'Auth\RegisterController@register');
Route::post('forgot-password', 'Auth\ForgotPasswordController@email');
Route::post('password-reset', 'Auth\ResetPasswordController@reset');
});

// Resource Endpoints
Route::group([
'middleware' => 'cors',
'prefix' => 'v1'
], function ($router) {
Route::apiResource('todo', 'TodoController');
});

// Not Found Route::fallback(function(){ return response()->json(['message' => 'Resource not found.'], 404); });```

9 Answers 9

5

I'm assuming this is in the routes/api.php file. If you declared all of this in the web routes file then there won't be any api prefixing it.

Check if your routes actually exist by running php artisan r:l in the console.

Sign up to request clarification or add additional context in comments.

2 Comments

These are the api routes in api files and they exists in php artisan r:l
Are you using auth for some of the routes? IIRC the api auth requires a 60 character string to be passed as a token by default.
3

You run only "php artisan route:clear"

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
By the way in my project on docker was RUN "php artisan route:clear" and after RUN "php artisan optimize" - this brocken my API project
1

In laravel 11 and higher, check Your bootstrap/app.php and add this line api: __DIR__.'/../routes/api.php', in withRouting method

for Example this is the default

`  ->withRouting(
    web: __DIR__ . '/../routes/web.php',
    commands: __DIR__ . '/../routes/console.php',
    health: '/up',
)`

then add the line to make it something like this

`  ->withRouting(
    web: __DIR__ . '/../routes/web.php',
    api: __DIR__.'/../routes/api.php',
    commands: __DIR__ . '/../routes/console.php',
    health: '/up',
)`

Comments

0

Because you don't sent a POST data. If you send a Url, you only send a GET. If you want to send POST verb, you can use a software like Postman to sent an URL http://xxxxxxx/api/v1/auth/login + POST data at the same time.

Route::post('login', 'Auth\LoginController@login'); <- POST, not found ... 404

4 Comments

Yes of course, but this error is triggered when I click on the login button of my react component. I send a post request.
_ You app is routing Route fallback -> 404 _ Check if Controller Auth\LoginController is OK _ Check if Method login in Auth\LoginController is Ok _ Check if Middleware \Illuminate\Session\Middleware\AuthenticateSession::class is working well in app\HTTP\Kernel.php _ Maybe is some code in Middleware that don't allows you route. it's because is after to routing. _
I've tried to add a get test route in API and the result is the same: 404
Are your routes cached? Try php artisan route:clear
0

Just as a future note to myself and in case it helps someone else, I was getting this error because I had a parameterized route that was taking precedence over another route with no parameters, but same signature, like this:

Route::prefix('post')->group(function () {
   Route::get('/', [PostController::class, 'index']);
   Route::get('/{post}', [PostController::class, 'show']);
   Route::get('/primary', [PostController::class, 'primary']);
   Route::get('/active3', [PostController::class, 'active3']);
   Route::get('/all', [PostController::class, 'index']);
});

Now whenever I tried to access /post/primary, it would return 404, even though the route is there and was even listing in php artisan route:list command results.

All of sudden it occurred to me that Laravel was search for a matching route in the order in which I have specified them, so post/{post} is a perfect match for post/primary (the word primary matches the parameter {post}) and therefore it goes to show function, which then fails because there is no Post with the id primary in the table and returns 404 (this is a Laravel feature called type-hinting).

The solution is to either move your parameterized route below the non-parameterized routes, so that those routes take precedence, or to introduce a where filter in your parameterized route, so that it matches numbers only, like this:

Route::get('/{post}', [PostController::class, 'show'])->whereNumber('poll');

Comments

0

In my case, I added following code in bootstrap/app.php inside withRouting function.

api: __DIR__.'/../routes/api.php',

And it worked.

Considering: You already have executed php artisan install:api

Comments

0

In my case, I didn't realise that the routes in the api.php file, by default automatically have the /api prefix. I was manually adding it in a group, so my routes were actually being defined as /api/api/path/to/route.

Comments

0

From Laravel 11, Laravel doesn't include an api.php by default. So we need to install it. Simply, we can create it using the below command.

php artisan install:api

This command will set everything.

Comments

-1

This could be due to php's opcache caching the routes files.

This happened to me and was difficult to debug because:

When I did php artisan route:list through the cli, it would show the correct information (as the php cli does not share opcache pool with php-fpm)

When traffic came through php-fpm, it would use old opcache.

The opcache was not cleared in my case, as I do zero downtime deployments using symlinks.

My solution was to have my deploy script restart php-fpm service.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.