Lee Pownall

Prefix Laravel Passport Routes

Outdated!

Warning! This post is old and may contain out of date advice.

Out of the box Passport urls are under oauth/*. How can you alter these?

To add Passport routes you need to add Passport::routes() to the boot method of AuthServiceProvider.

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
6use Laravel\Passport\Passport;
7 
8class AuthServiceProvider extends ServiceProvider
9{
10 public function boot()
11 {
12 Passport::routes();
13 }
14}

If you then run php artisan route:list --compact you'll see the routes.

1+----------+-----------------+---------------------------------------------------------------------------+
2| Method | URI | Action |
3+----------+-----------------+---------------------------------------------------------------------------+
4| GET|HEAD | oauth/authorize | Laravel\Passport\Http\Controllers\AuthorizationController@authorize |
5| POST | oauth/authorize | Laravel\Passport\Http\Controllers\ApproveAuthorizationController@approve |
6| DELETE | oauth/authorize | Laravel\Passport\Http\Controllers\DenyAuthorizationController@deny |
7| POST | oauth/clients | Laravel\Passport\Http\Controllers\ClientController@store |
8| GET|HEAD | oauth/clients | Laravel\Passport\Http\Controllers\ClientController@forUser |

To prefix these with api you can do the following:

1<?php
2 
3Passport::routes(null, [
4 'prefix' => 'api/oauth',
5]);

Out of the box Passport urls are under oauth/*. How can you alter these?

To add Passport routes you need to add Passport::routes() to the boot method of AuthServiceProvider.

1<?php
2 
3namespace App\Providers;
4 
5use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
6use Laravel\Passport\Passport;
7 
8class AuthServiceProvider extends ServiceProvider
9{
10 public function boot()
11 {
12 Passport::routes();
13 }
14}

If you then run php artisan route:list --compact you'll see the routes.

1+----------+-----------------+---------------------------------------------------------------------------+
2| Method | URI | Action |
3+----------+-----------------+---------------------------------------------------------------------------+
4| GET|HEAD | oauth/authorize | Laravel\Passport\Http\Controllers\AuthorizationController@authorize |
5| POST | oauth/authorize | Laravel\Passport\Http\Controllers\ApproveAuthorizationController@approve |
6| DELETE | oauth/authorize | Laravel\Passport\Http\Controllers\DenyAuthorizationController@deny |
7| POST | oauth/clients | Laravel\Passport\Http\Controllers\ClientController@store |
8| GET|HEAD | oauth/clients | Laravel\Passport\Http\Controllers\ClientController@forUser |

To prefix these with api you can do the following:

1<?php
2 
3Passport::routes(null, [
4 'prefix' => 'api/oauth',
5]);

Or if you are running PHP 8 you can use named arguments.

1<?php
2 
3Passport::routes(options: [
4 'prefix' => 'api/oauth',
5]);

If you run php artisan route:list --compact again you will see:

1+----------+---------------------+---------------------------------------------------------------------------+
2| Method | URI | Action |
3+----------+---------------------+---------------------------------------------------------------------------+
4| GET|HEAD | api/oauth/authorize | Laravel\Passport\Http\Controllers\AuthorizationController@authorize |
5| POST | api/oauth/authorize | Laravel\Passport\Http\Controllers\ApproveAuthorizationController@approve |
6| DELETE | api/oauth/authorize | Laravel\Passport\Http\Controllers\DenyAuthorizationController@deny |
7| POST | api/oauth/clients | Laravel\Passport\Http\Controllers\ClientController@store |
8| GET|HEAD | api/oauth/clients | Laravel\Passport\Http\Controllers\ClientController@forUser |