Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phayes0289's avatar

API Routes Are Not Working

I am having a problem with my API routes that I cannot figure out what is wrong.

The following is my very brief route file for my API:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\CadIncidentController;
use App\Models\CadIncident;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::post('/cad_incidents', [CadIncidentController::class, 'storeOrUpdate']);

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

When I use Postman to test the API using “ https://mysite/api/cad_incidents” as my endpoint, I am getting a 404 error. When I browse to “https://mysite/api/cad_incidents”, it also returns a 404.

This is what the controller looks like that should be handling the endpoint.

<?php

namespace App\Http\Controllers;

use App\Models\CadComment;
use App\Models\CadIncident;
use App\Models\Hydrant;
use App\Models\Lookup;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CadIncidentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */

    public function storeOrUpdate(Request $request)
    {
        $requestData = $request->validate([
            // Define validation rules for each field (e.g., required, date, etc.)
            // Example:
            'incidentid' => 'required|unique:cad_incidents',
            'incident_date' => 'required|date',
            'incident_type' => 'required',
            'cad_incident_number' => 'required',
            // Add validation rules for other fields accordingly.
        ]);

        // Parse JSON data from the request (assuming JSON data is in the request body)
        $jsonData = $request->json()->all();

        // Merge the parsed JSON data with the validated request data
        $requestData = array_merge($requestData, $jsonData);

        // Find or create the incident based on the 'incidentid'
        $incident = CadIncident::updateOrCreate(['incidentid' => $requestData['incidentid']], $requestData);

        return response()->json(['message' => 'Incident saved successfully', 'data' => $incident], 201);
    }

}

What am I doing wrong?

0 likes
8 replies
LaryAI's avatar
Level 58

The issue seems to be with the route configuration. The route file appears to be correct, but the problem might be with the route caching.

To resolve this issue, you can try clearing the route cache by running the following command in your terminal:

php artisan route:clear

If that doesn't work, you can also try clearing the application cache by running:

php artisan cache:clear

Additionally, make sure that your web server is properly configured to handle the API routes. If you are using Apache, make sure that the .htaccess file is present in the public directory and the mod_rewrite module is enabled.

If the issue still persists, you can try running the application using the built-in PHP development server to see if the routes work correctly. Run the following command in your terminal:

php artisan serve

Then, try accessing the API endpoint again using Postman or your browser.

If none of the above solutions work, please provide more information about your Laravel and server environment, as well as any error messages you are receiving, so that we can further assist you.

phayes0289's avatar

@LaryAI The .htaccess is there. It looks like this:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Atul27's avatar

Check request type in postman also check if your route service provider contains this code with prefix(api)

        Route::middleware('api')
            ->prefix('api') 
            ->group(base_path('routes/api.php'));

you can check same by hitting command :- php artisan route:list

1 like
thinkverse's avatar

I see two issues here. First, as others have mentioned, you're trying to access a POST route with a GET request.

To hit the endpoint in the browser. You need to call it either via a <form method="POST"> or optionally by using JavaScript with fetch or axios. Using Postman, set the method to POST in the drop-down next to where you enter the URL. That solves the first issue of trying to hit the endpoint correctly.

The second issue is that CadIncidentController doesn't have the correct namespace. It's missing the \Api at the end. So even hitting the endpoint correctly will most likely result in an error. This can be fixed in two ways. Update the use statement in api.php to look at the correct namespace or update the namespace in the CadIncidentController controller.

What you choose there is up to you, but I would go with updating the namespace in the controller.

1 like
shawinfra's avatar

Good question.

On my AI-todo app I put my Supabase calls in my Next.js backend, and the front end calls my Next.js API routes. e.g. using fetch("/api/getData") in the client.

On Vercel it is incredibly fast using Supabase this way - definitely not any speed issues. I've been addicted to clicking reload in the Chrome dev tools "Network" tab to study the speeds of my APIs.

I have not explicitly set up any caching but there seems to be some good caching by default - even over the double API routes (one to Next.js backend and the second to Supabase).

My main reason for doing this is speed - the more logic I can put in the backend then the smaller the frontend JS required to load the app on the client.

You could also use getServerSideProps but it depends on your use case. For lots of user specific data I find it better to make Next.js API routes.

1 like
mariaJans's avatar

add api: DIR . '/../routes/api.php', in your bootstrap/app

it should work

2 likes

Please or to participate in this conversation.