laravel/passportのrouteのprefixをデフォルトのoauth/から任意のルートに変更する【Laravel5.5】

## 概要

LaravelでAPI認証する際に利用するライブラリ、laravel/passportのrouteをデフォルトの `oauth/` から変更する方法。

※当記事ではlaravel/passportの導入方法は触れてれていないので、基本的な使い方、導入方法はこちらの記事を参照

Laravel5.5でAPI認証のパッケージ(Laravel Passport)を利用する - Qiita
https://qiita.com/niiyz/items/fffff94acb6061ecc9d4

## 前提

通常、laravel/passportをinstallして各種設定をしたら、以下のようなrouteが生成される。

```
# php artisan route:list
|        | POST      | oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\ApproveAuthorizationController@approve  | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\AuthorizationController@authorize       | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\DenyAuthorizationController@deny        | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | oauth/clients                           |                 | Laravel\Passport\Http\Controllers\ClientController@store                  | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/clients                           |                 | Laravel\Passport\Http\Controllers\ClientController@forUser                | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | oauth/clients/{client_id}               |                 | Laravel\Passport\Http\Controllers\ClientController@destroy                | Barryvdh\Cors\HandleCors::class,web,auth |
|        | PUT       | oauth/clients/{client_id}               |                 | Laravel\Passport\Http\Controllers\ClientController@update                 | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/personal-access-tokens            |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@forUser   | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | oauth/personal-access-tokens            |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@store     | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | oauth/personal-access-tokens/{token_id} |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@destroy   | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/scopes                            |                 | Laravel\Passport\Http\Controllers\ScopeController@all                     | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | oauth/token                             |                 | Laravel\Passport\Http\Controllers\AccessTokenController@issueToken        | Barryvdh\Cors\HandleCors::class,throttle |
|        | POST      | oauth/token/refresh                     |                 | Laravel\Passport\Http\Controllers\TransientTokenController@refresh        | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/tokens                            |                 | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@forUser | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | oauth/tokens/{token_id}                 |                 | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@destroy | Barryvdh\Cors\HandleCors::class,web,auth |
```
`oauth/` というrouteがいくつか生成されている。
laravelでは通常、apiは `api/` というrouteになるため、こちらのrouteに関しても`api/` にしたい。

## 手順

`app_name/app/Providers/AuthServiceProvider.php` の `boot()` を変更する。

デフォルトは以下のようになっている。

```php
<?php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}
```

このファイルの

```php
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
```
を

```php
    public function boot()
    {
        $this->registerPolicies();
        $callback = null;
        $options = [
            'prefix' => 'api/oauth',
            'namespace' => '\Laravel\Passport\Http\Controllers',
        ];
        Passport::routes($callback, $options);
    }
```
にする。

これで、routeのprefixが `oauth/` から　`api/oauth` に変更された。

`php artisan route:list` で確認するとこんな感じ。

```
# php artisan route:list

|        | POST      | api/oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\ApproveAuthorizationController@approve  | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\AuthorizationController@authorize       | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | api/oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\DenyAuthorizationController@deny        | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/clients                           |                 | Laravel\Passport\Http\Controllers\ClientController@forUser                | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | api/oauth/clients                           |                 | Laravel\Passport\Http\Controllers\ClientController@store                  | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | api/oauth/clients/{client_id}               |                 | Laravel\Passport\Http\Controllers\ClientController@destroy                | Barryvdh\Cors\HandleCors::class,web,auth |
|        | PUT       | api/oauth/clients/{client_id}               |                 | Laravel\Passport\Http\Controllers\ClientController@update                 | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/personal-access-tokens            |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@forUser   | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | api/oauth/personal-access-tokens            |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@store     | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | api/oauth/personal-access-tokens/{token_id} |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@destroy   | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/scopes                            |                 | Laravel\Passport\Http\Controllers\ScopeController@all                     | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | api/oauth/token                             |                 | Laravel\Passport\Http\Controllers\AccessTokenController@issueToken        | Barryvdh\Cors\HandleCors::class,throttle |
|        | POST      | api/oauth/token/refresh                     |                 | Laravel\Passport\Http\Controllers\TransientTokenController@refresh        | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/tokens                            |                 | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@forUser | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | api/oauth/tokens/{token_id}                 |                 | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@destroy | Barryvdh\Cors\HandleCors::class,web,auth |
```