skip to Main Content

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

4

Answers


  1. Chosen as BEST ANSWER

    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. Passport Installation Look at the example I have below of a possible mistake.

    enter image description here

    Look inside your User Model and make sure you have "use LaravelPassportHasApiTokens;" NOT "use LaravelSanctumHasApiTokens;"

    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, "LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::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 AppHttpControllersversion1UserController;" 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


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

    Login or Signup to reply.
  3. Pl check routes by php artisan route:list. Also check your APP_URL in. ENV File

    Login or Signup to reply.
  4. Also you should check the /config/app.php and the reference to AppProvidersRouteServiceProvider::class if is missing from the providers.

    After adding the RouteServiceProvider to the /config/app.php the routes will work again.

    For the original Laraval version of the file check here: https://github.com/laravel/laravel/blob/a6c68c24c9938beef0128c3288502b8fbdf8e93d/config/app.php#L178

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search