【Laravel】アクセスしてきたドメインによってルーティングをわける

例えばフロントのWebページと管理画面を一つのLaravelで管理するけど、ドメインが別々な場合。アプリケーションにアクセスしてきたドメインによってルーティングを分ける時のメモ。

(1) まずアクセスしてくるドメインを「.env」ファイルに書き込む

DOMAIN_FRONT=example-front.co.jp
DOMAIN_ADMIN=example-admin.co.jp

(2) 「config」配下に以下の内容で新規ファイル「domain.php」を作成

<?php

return [

    'front' => env('DOMAIN_FRONT', 'Laravel'),
    'admin' => env('DOMAIN_ADMIN', 'Laravel'),

];

(3) 「app/Providers/RouteServiceProvider.php」にフロントと管理画面(ADMIN)用のルートを追記

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapFrontRoutes();

        $this->mapAdminRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

    /**
     * Define the "front" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapFrontRoutes()
    {
        Route::namespace($this->namespace)
             ->group(base_path('routes/front.php'));
    }

    /**
     * Define the "admin" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapAdminRoutes()
    {
        Route::namespace($this->namespace)
             ->group(base_path('routes/admin.php'));
    }
}

(4) 「routes/web.php」をコピーし、フロント・管理画面用のそれぞれのルートファイルを作成し、必要なルーティング情報を記入
フロントページ用:routes/front.php
・管理画面用:routes/admin.php

(5) また、「app/Http/Controllers」フォルダや「resources」フォルダもfront, adminなどでフォルダ分けするとわかりやすい

これで「example-front.co.jp」からアクセスがあった場合は「routes/front.php」を、「example-admin.co.jp」からアクセスがあった場合は「routes/admin.php」を見に行くようにできた。サーバー側のvhostsで分けてLaravelプロジェクトを何個も作成する無駄が省ける!お試しあれ~