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.
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.
For those that in case of these scenarios:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With