I’m working on a Laravel API project, and my /api/events route is returning a 404 error even though I’ve set up the EventController and the route in routes/api.php. Despite having the server running correctly and seeing logs for the route, it still doesn’t work, and the route doesn’t appear when I run php artisan route:list.
I have tried defining my API routes in the routes/api.php file and set up the EventController in the Http/Controllers/Api directory. Here’s the relevant code for my setup:
API Route in routes/api.php:
`<?php
use AppHttpControllersApiAttendeeController;
use AppHttpControllersApiEventController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('events', EventController::class);
Route::apiResource('events.attendees', AttendeeController::class)
->scoped(['attendee' => 'event']);
Route::get('/test', function () {
return response()->json(['message' => 'Test route']);
});`
EventController in Http/Controllers/Api/EventController.php:
`<?php
namespace AppHttpControllersApi;
use AppHttpControllersController;
use IlluminateHttpRequest;
class EventController extends Controller
{
public function index()
{
return response()->json(['message' => 'Events listed successfully']);
}
public function store(Request $request) { /* ... */ }
public function show(string $id) { /* ... */ }
public function update(Request $request, string $id) { /* ... */ }
public function destroy(string $id) { /* ... */ }
}`
RouteServiceProvider.php:
`<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesRoute;
class RouteServiceProvider extends ServiceProvider
{
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace('AppHttpControllersApi')
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
}
`
I expected the /api/events route to display data from the EventController but instead received a 404 error. The server logs show the route is being accessed, but it still returns a 404. I have manually created both api.php and RouteServiceProvider.php as they were missing from my project.
Despite all efforts like clearing configuration cache and publishing vendor files, the php artisan route:list command does not display my API routes, which is unusual.
this is my new tried :
**app.php **
<?php
use IlluminateFoundationApplication;
use IlluminateFoundationConfigurationExceptions;
use IlluminateFoundationConfigurationMiddleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php', // This handles the root ("/") route.
commands: __DIR__.'/../routes/console.php',
health: '/up'
)
->withRouting(
api: __DIR__.'/../routes/api.php', // This handles API routes.
apiPrefix: 'api/' // Ensure the prefix is used only for API routes.
)
->withMiddleware(function (Middleware $middleware) {
// Add any middlewares here if necessary.
})
->withExceptions(function (Exceptions $exceptions) {
// Add any exception handling here if necessary.
})->create();
the results of "php artisan route:list":
GET|HEAD api/events ....................................................................... events.index › ApiEventController@index
POST api/events ....................................................................... events.store › ApiEventController@store
GET|HEAD api/events/{event} ................................................................. events.show › ApiEventController@show
PUT|PATCH api/events/{event} ............................................................. events.update › ApiEventController@update
DELETE api/events/{event} ........................................................... events.destroy › ApiEventController@destroy
GET|HEAD api/events/{event}/attendees ........................................ events.attendees.index › ApiAttendeeController@index
POST api/events/{event}/attendees ........................................ events.attendees.store › ApiAttendeeController@store
GET|HEAD api/events/{event}/attendees/{attendee} ............................... events.attendees.show › ApiAttendeeController@show
PUT|PATCH api/events/{event}/attendees/{attendee} ........................... events.attendees.update › ApiAttendeeController@update
DELETE api/events/{event}/attendees/{attendee} ......................... events.attendees.destroy › ApiAttendeeController@destroy
GET|HEAD api/test ..................................................................................................................
GET|HEAD api/user ..................................................................................................................
GET|HEAD sanctum/csrf-cookie ..................................... sanctum.csrf-cookie › LaravelSanctum › CsrfCookieController@show
GET|HEAD storage/{path} .............................................................................................. storage.local
2
Answers
I recently encountered a problem with Laravel 11 when working with API routes. I had manually added an API-related file, which caused Laravel to not recognize it, even though the file was visible. When I tried to run my API, the routes wouldn't work, and attempts to redownload the file through php artisan install:api failed with an error saying the file already existed.
Solution:
In Laravel 11, when starting a project with API functionality, you must download the necessary API setup using:
Do not manually add or create the file yourself. If you do, Laravel will see the file but won’t recognize or use it properly, leading to issues with routing. Additionally, trying to redownload the file using the artisan command will fail because Laravel believes the file already exists, even though it isn’t functioning correctly.
If you face this problem:
Remove any manually added API files.
Run the command:
php artisan install:api
This solved my issue, and I hope it helps anyone else facing the same problem!
Since you’re using Laravel 11, in order to register api routes in to the application, you need to use:
Also,
RouteServiceProvider
is no longer used in Laravel 11. Route configuration is now moved tobostrapp/app.php
Note: If the above command doesn’t work, you can manually register api routes in the
bostrapp/app.php
:Read more of official documentation