laravel api route not found but exists in route list
Asked Answered
D

4

8

I'm trying to add a patch route to routes/api.php but I get "route not found" even after trying route:cache. it's registered in route:list and other routes in that scope are working.

this is my code:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::group([
    'prefix' => 'v1',
    'namespace' => 'App\Http\Controllers\Api\V1',
], function() {

    Route::group([
        'prefix' => '/users',
    ], function() {
        Route::get('/{user}', 'UsersController@show');
        Route::patch('/{user}/updateStatus', 'UsersController@updateStatus');
    });

});

and this is my code in controller action:


<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller {
    public function updateStatus(Request $request, User $user) {
         # my logic
    }
}

the route is registered as /api/v1/users/{user}/updateStatus but I get 404.

by the way, I'm using laravel 8 and php 7.3

Deceive answered 25/5, 2021 at 5:47 Comment(3)
Welcome to SO ... may be user is not exits and your using route model biding so it retruns 404 errorCynic
are you sending JSON raw data or form-data?Ingot
model binding is working fine, I have a show action as well and model binding works well in that case. also I'm using raw json format.Deceive
I
3

I think web server will automatically change url to lowercase so you must use lowercase in laravel route,so you must change "updateStatus" to "updatestatus" or "update-status"

Ion answered 1/6, 2021 at 8:27 Comment(1)
thank you. that was the problem. I changed the url to something else and that worked.Deceive
F
0

in this code if your $user with id you send to Api not exist , you will see 404 HTTP status code . and remember if you want to cache your routes you need to do this after any change in your routes to apply your change .

Fionafionna answered 25/5, 2021 at 7:0 Comment(2)
user is defined and show route for that specific user is working well. I even tried anonymous function in route definition and dd($request) but the route is not working and I get 404Deceive
@Deceive for anonymous function you must define all route parameters . did you check it ?Fionafionna
C
0

In your route prefix it should be "api/v1"

Route::group([
    'prefix' => 'api/v1',
    'namespace' => 'App\Http\Controllers\Api\V1',
], function() {

Also try

php artisan cache:clear
php artisan optimize
Curren answered 25/5, 2021 at 7:54 Comment(3)
the routes are in the routes/api.php and automatically they get api as prefix. also other routes are working. I changed the order of routes as well. could it be the problem?Deceive
You can try by changing order but that should not matter.Curren
this could be happening because you have used User $user as second param, you should use only $user. Try this it should do the trick.Curren
P
0

In the past I found that there can be also a wrong format on route parameter(s).

For eg.

Route::post('some/{some-param}/route', [Controller::class, 'method');

The hiphen will cause the issuee.

This will result into a 404 route not found error without indicating the cause - route param malformed.

Here can't find a warning about it - just saying alphabetic characters...

I think the code can be improved in terms of checking the uri correctness and gave to the silly dev :) a proper error message.

Parsifal answered 6/9 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.