laravel api route not found but exists in route list

Viewed 651

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

3 Answers

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"

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 .

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
Related