LoginSignup
24
20

More than 3 years have passed since last update.

【Laravel5】認識機能を実装した際に追加される「Auth::routes()」にオプションを指定してカスタマイズする

Last updated at Posted at 2019-07-19

php artisan make:authは便利だけど登録機能は別で作りたい

という場合があると思うので、標準で実装される登録機能のルーティングを外してみたりしてみます。
※Laravel5.7系以上

ソース

php artisan make:authを実行後、ルーティングに以下のコードが自動追加されると思うので、こいつに引数を渡していく

routes\web.php
Auth::routes();

ちなみに上記のルーティング情報は以下の通りです。
WS000045.JPG

Auth::routes()に対して以下のようにオプションで引数をbooleanで渡してやるとルーティングの設定をON/OFFできる

routes\web.php
Auth::routes([
    'verify'   => true, // メール確認機能(※5.7系以上のみ)
    'register' => false, // デフォルトの登録機能OFF
    'reset'    => true,  // メールリマインダー機能ON
]);

上記のルーティング情報は以下の通り。registerが消えてverifyが追加されていると思います。
WS000046.JPG

大本のソースは以下(laravel\framework\src\Illuminate\Routing\Router.php)にあります。
コードを見てもらったらわかると思いますが、デフォルトでregister(登録機能)とreset(パスワードリマインダー)はスカフォールディングした段階で有効になるように書かれています。
(以下のソースはLaravel5.8系なので、新機能のメール確認機能verifyが記述されていてかつデフォルトで無効になるようになっています)

laravel\framework\src\Illuminate\Routing\Router.php
    /**
     * Register the typical authentication routes for an application.
     *
     * @param  array  $options
     * @return void
     */
    public function auth(array $options = [])
    {
        // Authentication Routes...
        $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
        $this->post('login', 'Auth\LoginController@login');
        $this->post('logout', 'Auth\LoginController@logout')->name('logout');

        // Registration Routes...
        if ($options['register'] ?? true) {
            $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
            $this->post('register', 'Auth\RegisterController@register');
        }

        // Password Reset Routes...
        if ($options['reset'] ?? true) {
            $this->resetPassword();
        }

        // Email Verification Routes...
        if ($options['verify'] ?? false) {
            $this->emailVerification();
        }
    }

おわり

  • 使用してない機能はルーティングをOFFにして不要なコントローラーは削除しておきましょう
  • ただし、↑の引数で設定を切り替えられるのは5.7系以前は実装されてないみたいなので注意してください。
24
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
20