Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel 11 api routes
    laravel 11


Laravel 11 api routes


In Laravel 11, API routes are no longer included by default in the application skeleton. To enable API routing, you can run the `php artisan install:api` command[1][3]. This command will:

1. Install Laravel Sanctum, which provides a robust, yet simple API token authentication guard for authenticating third-party API consumers, SPAs, or mobile applications[5].

2. Create the `routes/api.php` file[1][5], where you can define your API routes. The routes in this file are stateless and are assigned to the `api` middleware group[5].

3. Configure the `api` prefix to be automatically applied to the API routes in the `bootstrap/app.php` file[5].

After running `php artisan install:api`, you can define your API routes in the `routes/api.php` file[1][2][4]. For example:

php
Route::prefix('v1')->group(base_path('routes/api_v1.php'));
Route::prefix('v2')->group(base_path('routes/api_v2.php'));

This allows you to version your API by separating routes into different files (`api_v1.php` and `api_v2.php`)[1][4]. You can then define the specific routes for each version in their respective files.

To list all the API routes in your application, you can run `php artisan route:list --path=api`[4][5]. This will display only the routes that begin with the `/api` prefix.

Citations:
[1] https://laravel-news.com/api-versioning-in-laravel-11
[2] https://stackoverflow.com/questions/78188214/unable-to-locate-the-api-php-route-file-in-laravel-11
[3] https://www.youtube.com/watch?v=agUnf89SAOg
[4] https://www.youtube.com/watch?v=Tl_BbMDg-2o
[5] https://laravel.com/docs/11.x/routing