Home > front end >  404 - Not Found Passport Api Route That Exists Laravel 8
404 - Not Found Passport Api Route That Exists Laravel 8

Time:09-17

I installed passport and have a route in the api.php file. The web route works but the api routes always give me a 404 - Not Found in Postman.

This is my route

enter image description here

This is my UserController Function

enter image description here

CodePudding user response:

I was stuck with this for a week. There can be many things that can cause this issue.

1 - Make sure you installed passport correctly.

It appears laravel 8 comes with "Sanctum" pre-installed so there is a likelihood that you will make mistakes with some of passport's installation requirements. Go through the installation again. enter image description here

Look inside your User Model and make sure you have "use Laravel\Passport\HasApiTokens;" NOT "use Laravel\Sanctum\HasApiTokens;"

enter image description here

2- If you are not using "Sanctum", you are better off removing it from your project

Go to the search on the left side bar of VSCODE and search for "Sanctum".

enter image description here

Click on "composer.json", go to the require section and carefully remove "Sanctum" requirement. Also check all the other search results to make sure you are not using Sanctum in a place where it should be passport. Remove them if need be.

enter image description here

3- In your terminal, type the command below to Update composer and generate a new lock file

composer update

4- If you moved your User model to a new folder (eg: version1) like I did, go to "config/auth.php" and update the providers for user with the correct path of the User model

enter image description here

5- Make sure in the api section of app/Http/Kernel.php, "\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class" is commented out.

enter image description here

6- Go to terminal, clear your routes from cache, list them with the commands below and check if the route you listed in api.php shows there.

php artisan route:clear

php artisan route:list

If "php artisan route:list" shows an error, then make sure your route is declared correctly. For clarity sake, make sure you use the full path of your controller and crosscheck your UserController to make sure of the function matches(they are case sensitive) exactly with what is in your api.php file. Also, if you are using a controller in your route, declare it above "use App\Http\Controllers\version1\UserController;" like shown in the image below. Repeat "php artisan route:clear" and "php artisan route:list" in terminal to make sure there is no error and your route is listed.

enter image description here

7- Make sure the url you are calling in postman is correct and the method is also correct.

Remember, if your url in api.php is "/v1/user/register" then in postman, you should have "api/v1/user/register" in postman. That means your final url will be "http://myvhostname.local/api/v1/user/register"

Remember, if you declared your route in api.php to be a "get" then make sure it is a "get" in postman. If it's a post, make sure they match in postman too.

Go to the "headers" section of postman and include "Accept=application/json" as shown below

enter image description here

8- If you still have a 404-Not Found, open your vhost file "httpd-vhosts.conf" in "Applications >> XAMPP >> xamppfiles >> etc >> extra" and make sure your vhost is declared correctly as below. Improper declaration can make web routes work but api routes won't. Pay close to "Allowoverride All".

enter image description here

Your routes should work now. If this worked for you, an upvote would be appreciated

CodePudding user response:

Just a quick one. If the error code is a 404, double-check you are making a POST request in Postman, not a GET.

CodePudding user response:

Pl check routes by php artisan route:list. Also check your APP_URL in. ENV File

  • Related