Laravelをマルチドメインで利用時に、パブリックフォルダもドメイン単位で切り替える

今まで一つのLaravelフレームワークをマルチドメインで利用する際、パブリックフォルダはほぼ利用していない場合共通のもの(標準のフォルダ)を使えば事足ります。

複数のドメインやサブドメインでパブリックフォルダ(ドキュメントルート)を分ける場合の方法です。

パブリックフォルダをそれぞれ用意する

./public

これがデフォルトのフォルダですが、

Laravel配下に作成し、Gitなんかでまとめて管理する場合は、同じ階層に、

mkdir public_hogehoge

などでフォルダを作成し、apacheなどからドメインやサブドメイン単位でのドキュメントルートをそれぞれ任意のものに指定してください。

新規フォルダにファイルをコピー

このままだと public_hogehoge にきたアクセスからlaravelが起動しませんので、

publicフォルダ内のファイルをまるっと新規で作ったフォルダにコピーします。

cd public
cp * ../public_hogehoge

こんな感じかな。

もともと新規フォルダ作成時に、フォルダごとコピーしてもいいです。

.htaccess
index.php

ちなみに、この2つのファイルだけあれば他のファイルはなくても動きます。

publicと同じ階層に新規フォルダを作成した場合は、この段階で作業完了です。

index.phpを編集

publicと違った階層に新規パブリックフォルダを持ってきた場合は、index.phpの編集が必要になります。

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

この2つのパスを

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require '/var/www/laravel/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once '/var/www/laravel/bootstrap/app.php';

こんな感じでちゃんと通してあげるだけ。

__DIR__ からの相対パスでも、絶対パスでもどちらでも大丈夫です。