deepu sankar
deepu sankar

Reputation: 4455

Getting NotFoundHttpException error on Laravel API route

Added Route in Laravel Project

Route::middleware('auth:sanctum')->group(function () {

    Route::get('/user', function (Request $request) {
        return $request->user();
    });

    Route::middleware('token.type:apiclient')
        ->post('users/{user}/tokens', [TokenController::class, 'generateByUserId']);

    Route::resource('phoenix-users', PhoenixUserController::class)
        ->only(['store']);
});

I am getting this error. How can I solve this?

         {
         "message": "",
         "exception": 
         "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
          "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php",
         "line": 43,
         "trace": [
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
        "line": 162,
        "function": "handleMatchedRoute",
        "class": "Illuminate\\Routing\\AbstractRouteCollection",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
        "line": 647,
        "function": "match",
        "class": "Illuminate\\Routing\\RouteCollection",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
        "line": 636,
        "function": "findRoute",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
        "line": 625,
        "function": "dispatchToRoute",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
        "line": 166,
        "function": "dispatch",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 128,
        "function": "Illuminate\\Foundation\\Http\\{closure}",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php",
        "line": 67,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Barryvdh\\Debugbar\\Middleware\\InjectDebugbar",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/app/Http/Middleware/SecureHeaders.php",
        "line": 19,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "App\\Http\\Middleware\\SecureHeaders",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php",
        "line": 31,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php",
        "line": 40,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
        "line": 27,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php",
        "line": 86,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/fruitcake/laravel-cors/src/HandleCors.php",
        "line": 38,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fruitcake\\Cors\\HandleCors",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/fideloper/proxy/src/TrustProxies.php",
        "line": 57,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fideloper\\Proxy\\TrustProxies",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 103,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
        "line": 141,
        "function": "then",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
         },
         {
        "file": "/var/www/html/core-cdp- api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
        "line": 110,
        "function": "sendRequestThroughRouter",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/public/index.php",
        "line": 52,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
        },
        {
        "file": "/var/www/html/core-cdp-api/server.php",
        "line": 21,
        "function": "require_once"
      }
      ]
      }

Upvotes: 2

Views: 4641

Answers (3)

Haroun
Haroun

Reputation: 1

clear the route

php artisan route:clear

Upvotes: 0

Rens van Melis
Rens van Melis

Reputation: 43

This means that your application couldn't find the route.

First please check your route : make sure dictation and characters are correct

You can try to run the 'php artisan optimize' command. This will clear your cache.

Upvotes: 1

Kevin
Kevin

Reputation: 1195

As stated on the above comments, a 404 error means that the endpoint you provided couldn't match any of your routes. From a hard guess, if you are using api.php folder to register your routes you need to add /api in your endpoint as a prefix. If you don't want to do that, remove it on the RouteServiceProvider.php

Upvotes: 1

Related Questions