Help us understand the problem. What is going on with this article?

Laravelを複数ドメインで使ってみた

サービス中にURLが長くなってしまう部分があって、これを短縮URLで解決しよう...というとある事情でhogehoge.comで稼働していLaravelシステムにfuga.jpでアクセスする必要が出たので、そのときの技術メモ

環境

  • Laravel 5.8
  • PHP 7.2
  • MacOSローカル

実装

ドメインを識別するミドルウェアをたてて、それをRouteGroupに読み込ませるイメージ。

【補足】
最初、この辺とか見てRoute::domain()で実装していたんですが、route()url()で返ってくるURLが揃わず挙動がおかしかったので断念。

1.config/app.phpでドメイン名を定義

...
'test_domain' => env('TEST_DOMAIN', 'hogehoge.local'),
'short_test_domain' => env('SHORT_TEST_DOMAIN', 'fuga.local'),
'production_domain' => env('PROD_DOMAIN', 'hogehoge.com'),
'short_domain' => env('SHORT_DOMAIN', 'fuga.jp'),
...

2.2つのMiddlewareを用意

これはhogehoge.com

app/Http/Middleware/OnlyMainDomain.php
<?php

namespace App\Http\Middleware;

use Closure;

class OnlyMainDomain
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (
            config('app.short_test_domain') === request()->getHost() ||
            config('app.short_domain') === request()->getHost()
        ) {
            return \abort(403);
        }

        return $next($request);
    }
}

これはfuga.jp

app/Http/Middleware/OnlyShortDomain.php
<?php

namespace App\Http\Middleware;

use Closure;

class OnlyShortDomain
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (
            config('app.test_domain') === request()->getHost() ||
            config('app.production_domain') === request()->getHost()
        ) {
            return \abort(403);
        }

        return $next($request);
    }
}

Middlewareを登録します。

app/Http/Kernel.php
    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        ...
        'domain.short' => \App\Http\Middleware\OnlyShortDomain::class,
        'domain.main' => \App\Http\Middleware\OnlyMainDomain::class,
        ...
    ];

3.ルーティングでMiddlewareを実装

routes/web.php
Route::group(['middleware' => ['domain.main']], function () {
    Route::get('/', function () {
        return view('index');
    });
});

Route::group(['middleware' => ['domain.short'], 'namespace' => 'Short'], function () {
    Route::get('/s/{key}', 'MainController@index');
});

どうなるか

これで、互いの領分を超えたルートへアクセスすると403を返すようになりました。

欠点

  • 2つのドメインで同じルートを利用できない
    hogehoge.com/aaa/bbbにはMainController@hogefuga.jp/aaa/bbbにはMainController@fugaみたいなのはできない。

まとめ

こんな行き当たりばったりの構築はやめて、ちゃんと設計したい。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした