【Laravel】Routingの書き方のおすすめは？

## この記事の意図

LaravelのRoutingはなかなか自由に書くことができます。なので一旦どう書くといいんだ？
的なことが僕には怒っています（いえ、起こっています）なので個人的におすすめを披露。

## RoutingのBestを探すメリット

- 探しやすい
- 可読性があがることで管理しやすくなる
- セキュリティー面で確認しやすい

## RoutingのBestを探すデメリット
- たぶんないのでレッツBest
- つかれるのかも

## 結論
いろんなパターンを書くのが面倒なのでいきなり結論。
結論についてだけ解説します。

まず、著者の著書を紹介するサイトだと想定してください。
なので著者は著書を大量に持っています。

```PHP:web.php
<?php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Auth::routes(['verify' => true]);
// ダッシュボード
Route::get('/', 'HomeController@index')->name('home');
// 著者ページ
Route::resource('authors', 'AuthorController');
// 著者配下
Route::prefix('authors/{author}')->name('authors.')->group(function () {
    // 著者アカウントページ
    Route::prefix('accounts')->name('profile.')->group(function () {
        Route::get('email', 'AuthorAccountController@email')->name('email');
        Route::put('email', 'AuthorAccountController@updateEmail')->name('email.update');
        Route::get('password', 'AuthorAccountController@password')->name('password');
        Route::put('password', 'AuthorAccountController@updatePassword')->name('password.update');
    });
    // 著書ページ
    Route::resource('books', 'BookController');
});


```

middlewareは面倒ですがControllerに記載

```PHP:AuthorController
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Requests\Authors\UpdateRequest;
use App\Models\Author;

class AuthorController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['auth', 'verified']);
    }
}

```

`php artisan route:list` で見るとこんな感じ

```shell
+--------+-----------+------------------------------------+---------------------------------+------------------------------------------------------------------------+-------------------------------------------------+
| Domain | Method    | URI                                | Name                            | Action                                                                 | Middleware                                      |
+--------+-----------+------------------------------------+---------------------------------+------------------------------------------------------------------------+-------------------------------------------------+
|        | GET|HEAD  | /                                  | home                            | App\Http\Controllers\HomeController@index                              | web,auth,verified                               |
|        | GET|HEAD  | _ignition/assets                   |                                 | Facade\Ignition\Http\Controllers\IgnitionAssetsController              | Facade\Ignition\Http\Middleware\IgnitionEnabled |
|        | POST      | _ignition/execute-solution         |                                 | Facade\Ignition\Http\Controllers\ExecuteSolutionController             | Facade\Ignition\Http\Middleware\IgnitionEnabled |
|        | GET|HEAD  | _ignition/health-check             |                                 | Facade\Ignition\Http\Controllers\HealthCheckController                 | Facade\Ignition\Http\Middleware\IgnitionEnabled |
|        | GET|HEAD  | _ignition/scripts/{script}         |                                 | Facade\Ignition\Http\Controllers\ScriptController                      | Facade\Ignition\Http\Middleware\IgnitionEnabled |
|        | POST      | _ignition/share-report             |                                 | Facade\Ignition\Http\Controllers\ShareReportController                 | Facade\Ignition\Http\Middleware\IgnitionEnabled |
|        | GET|HEAD  | _ignition/styles/{style}           |                                 | Facade\Ignition\Http\Controllers\StyleController                       | Facade\Ignition\Http\Middleware\IgnitionEnabled |
|        | POST      | authors                            | authors.store                   | App\Http\Controllers\AuthorController@store                            | web,auth,verified                               |
|        | GET|HEAD  | authors                            | authors.index                   | App\Http\Controllers\AuthorController@index                            | web,auth,verified                               |
|        | GET|HEAD  | authors/create                     | authors.create                  | App\Http\Controllers\AuthorController@create                           | web,auth,verified                               |
|        | GET|HEAD  | authors/{author}                   | authors.show                    | App\Http\Controllers\AuthorController@show                             | web,auth,verified                               |
|        | PUT|PATCH | authors/{author}                   | authors.update                  | App\Http\Controllers\AuthorController@update                           | web,auth,verified                               |
|        | DELETE    | authors/{author}                   | authors.destroy                 | App\Http\Controllers\AuthorController@destroy                          | web,auth,verified                               |
|        | PUT       | authors/{author}/accounts/email    | authors.profile.email.update    | App\Http\Controllers\AuthorAccountController@updateEmail               | web,auth,verified                               |
|        | GET|HEAD  | authors/{author}/accounts/email    | authors.profile.email           | App\Http\Controllers\AuthorAccountController@email                     | web,auth,verified                               |
|        | PUT       | authors/{author}/accounts/password | authors.profile.password.update | App\Http\Controllers\AuthorAccountController@updatePassword            | web,auth,verified                               |
|        | GET|HEAD  | authors/{author}/accounts/password | authors.profile.password        | App\Http\Controllers\AuthorAccountController@password                  | web,auth,verified                               |
|        | POST      | authors/{author}/books             | authors.books.store             | App\Http\Controllers\BookController@store                              | web,auth,verified                               |
|        | GET|HEAD  | authors/{author}/books             | authors.books.index             | App\Http\Controllers\BookController@index                              | web,auth,verified                               |
|        | GET|HEAD  | authors/{author}/books/create      | authors.books.create            | App\Http\Controllers\BookController@create                             | web,auth,verified                               |
|        | DELETE    | authors/{author}/books/{book}      | authors.books.destroy           | App\Http\Controllers\BookController@destroy                            | web,auth,verified                               |
|        | PUT|PATCH | authors/{author}/books/{book}      | authors.books.update            | App\Http\Controllers\BookController@update                             | web,auth,verified                               |
|        | GET|HEAD  | authors/{author}/books/{book}      | authors.books.show              | App\Http\Controllers\BookController@show                               | web,auth,verified                               |
|        | GET|HEAD  | authors/{author}/books/{book}/edit | authors.books.edit              | App\Http\Controllers\BookController@edit                               | web,auth,verified                               |
|        | GET|HEAD  | authors/{author}/edit              | authors.edit                    | App\Http\Controllers\AuthorController@edit                             | web,auth,verified                               |
|        | POST      | email/resend                       | verification.resend             | App\Http\Controllers\Auth\VerificationController@resend                | web,auth,throttle:6,1                           |
|        | GET|HEAD  | email/verify                       | verification.notice             | App\Http\Controllers\Auth\VerificationController@show                  | web,auth                                        |
|        | GET|HEAD  | email/verify/{id}/{hash}           | verification.verify             | App\Http\Controllers\Auth\VerificationController@verify                | web,auth,signed,throttle:6,1                    |
|        | GET|HEAD  | login                              | login                           | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest                                       |
|        | POST      | login                              |                                 | App\Http\Controllers\Auth\LoginController@login                        | web,guest                                       |
|        | POST      | logout                             | logout                          | App\Http\Controllers\Auth\LoginController@logout                       | web                                             |
|        | POST      | password/email                     | password.email                  | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest                                       |
|        | GET|HEAD  | password/reset                     | password.request                | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest                                       |
|        | POST      | password/reset                     | password.update                 | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest                                       |
|        | GET|HEAD  | password/reset/{token}             | password.reset                  | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest                                       |
|        | GET|HEAD  | register                           | register                        | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest                                       |
|        | POST      | register                           |                                 | App\Http\Controllers\Auth\RegisterController@register                  | web,guest                                       |
+--------+-----------+------------------------------------+---------------------------------+------------------------------------------------------------------------+-------------------------------------------------+
```

何がいいか？

## 【No.1】MiddlewareはControllerへ
めっちゃ完結に言うと、Controllerとmiddlewareの結びつきが正しいかを判断しやすくするためです。
Routingに入れ子構造で書くと、どうしても関係性がわかりにくいし、そもそも目に入りにくいのでButと考えました。

## 【No.2】nameなどの表記は個別に
この辺りの話ですね。

```PHP
Route::prefix('authors/{author}')->name('authors.')->
```
理由は明確、統合開発ツールでみやすい！！
こちらの書き方と比較

```PHP
Route::get('login', [
    'as' => 'login',
    'uses' => 'Auth\LoginController@showLoginForm'
]);
```
具体的な値も、その要素名も全部同じ色でわかりにくい！

## 以上！

というわけで個人的なRouting Bestでした！
ご意見・批判・編集依頼などすべてWelcomeです笑
ではまた