have you enabled the api.php routes in your ServiceProvider or in bootstrap/app.php if using Laravel 11?
Route Not Found for API Endpoint in Laravel
I'm encountering an issue where a specific API route is not being found on my Laravel application. The route in question is supposed to fetch invoice data using the GET method. Despite having set up the route and controller, it appears that the route is not being registered correctly, resulting in a 404 Not Found error when trying to access it.
API Route Definition: The route is defined in routes/api.php:
php Copy code // routes/api.php use App\Http\Controllers\APIController;
Route::get('/get-invoice-data/{invoice_id}', [APIController::class, 'getInvoiceData']); Controller Method: The controller method for handling the request:
// app/Http/Controllers/APIController.php namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Models\Invoice; use Validator;
class APIController extends Controller { public function getInvoiceData(Request $request, $invoice_id) { $validator = Validator::make(['invoice_id' => $invoice_id], [ 'invoice_id' => 'required|exists:invoices,invoice_code', ]);
if ($validator->fails()) {
return response()->json([
'status' => false,
'message' => 'Invoice not found or invalid invoice ID.',
'data' => null,
], 404);
}
try {
$invoice = Invoice::with(['merchant', 'brand', 'service'])
->where('invoice_code', $invoice_id)
->first();
if (!$invoice) {
return response()->json([
'status' => false,
'message' => 'Invoice not found.',
'data' => null,
], 404);
}
return response()->json([
'status' => true,
'message' => 'Invoice data retrieved successfully.',
'data' => $invoice,
], 200);
} catch (\Exception $e) {
return response()->json([
'status' => false,
'message' => 'An error occurred while retrieving invoice data.',
'error' => $e->getMessage(),
], 500);
}
}
}
Issue: When making a request to the endpoint, I receive the following error:
{ "message": "The route api/get-invoice-data/JT2Z5HULFI20240730052750 could not be found.", "exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException", ... } Route List Check: The route list output does not include the expected route:
php artisan route:list The list shows routes related to other functionalities but does not include /api/get-invoice-data/{invoice_id}.
Troubleshooting Steps Taken:
Verified the route definition in routes/api.php. Checked the controller namespace and method. Cleared route and config cache:
php artisan route:clear php artisan config:clear
Please or to participate in this conversation.