Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to locate the api.php route file in Laravel 11

I am attempting to integrate Laravel 11 with React.js for data retrieval and transmission between the two. However, I cannot locate the routes/api.php file in the latest version of Laravel.

I have searched for others experiencing the same issue, but I have yet to find any similar cases since Laravel 11 was only released a week ago.

like image 788
Anas Oudadsse Avatar asked Sep 05 '25 14:09

Anas Oudadsse


2 Answers

https://laravel.com/docs/11.x/routing#api-routes

If your application will also offer a stateless API, you may enable API routing using the install:api Artisan command:

php artisan install:api

[...] In addition, the install:api command creates the routes/api.php file.

* Also ensure you have api: __DIR__ . '/../routes/api.php', included in the /bootstrap/app.php file.

like image 117
ericmp Avatar answered Sep 08 '25 04:09

ericmp


For those that in case of these scenarios:

  • used the breeze kit with inertia and vue3 and refactor that to use vue3 vanilla and rip out inertia (I know this is a bit round about aproach).
  • Another scenario could be that you mixed and matched code from different tutorials and ended up with this boostrap/app.php.
  • Or you used AI to add code snippets, which can confuse laravel versions and do things a bit inconsistent with best practices.

In any case, the issue is that Laravel doesn't register the api middleware when there's a 'using' clause in the bootstrap/app.php. artisan install:api doesn't detect this and it adds the path to the api router but inside the withRouting() it's ignored.

The breeze kit has this for the web route in the using clause:

// bootstrap/app.php
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
    using: function () {
        Route::middleware('web')
            ->namespace('App\Http\Controllers')
            ->group(base_path('routes/web.php'));

Just add the matching api registration like this:

        Route::middleware('api')
            ->namespace('App\Http\Controllers')
            ->prefix('api')
            ->group(base_path('routes/api.php'));

Another issue I came across is permissions in docker, when artisan install:api is done in wsl using php artisan install:api instead of sail artisan install:api you could end up with permissions being incorrect and the api.php won't load.

Log into the container with (replace laravel.test with your container name)

docker container exec -u 0 -it laravel.test bash
chmod -R 777 /var/www/html/routes/

Hopefully this is useful for those that got caught up in a less than ideal Laravel install.

like image 34
imqqmi Avatar answered Sep 08 '25 02:09

imqqmi