Laravelをサブディレクトリで公開する話
今回は、先日の郵便番号APIで思いつきました。
Laravelで一番はてなと思う箇所はどこでしょう。
そう、プロジェクトがドキュメントルートではなくプロジェクト下のpublicディレクトリがドキュメントルートであることが一番引っ掛かるところです。
要するに、/var/www/htmlの部分がpublicになればOKとなりますね。
そうすると、www.mmpp.orgでlaravelアクセスならできますが、www.mmpp.org/appで公開するには?
/ルートをlaravelプロジェクトにして、publicディレクトリを… app?
sub_appなんてきたら… とか…
そんなの切れば良いんですよ!と言うかもしれませんが、問題は切れないSAKURAインターネットさんでの問題でしたw
するってぇ〜と… となります;;
ここで登場するのが、今回のpublicのindex.php直書きですw
public下を全て、欲しいサブディレクトリをサブディレクトリで保存します。
次に、laravelをサイトに保存します。
私の場合はユーザルートにprojectを作成してそれぞれ保存しています
次にindex.phpを編集します、デフォルト状態は親ディレクトリを見に行っていますので、そいつをプロジェクトに直接見せます
<?php use Illuminate\Contracts\Http\Kernel; use Illuminate\Http\Request; define('LARAVEL_START', microtime(true)); /* |-------------------------------------------------------------------------- | Check If Application Is Under Maintenance |-------------------------------------------------------------------------- | | If the application is maintenance / demo mode via the "down" command we | will require this file so that any prerendered template can be shown | instead of starting the framework, which could cause an exception. | */ if (file_exists('/home/mmpp/project/postal_api/storage/framework/maintenance.php')) { require '/home/mmpp/project/postal_api/storage/framework/maintenance.php'; } /* |-------------------------------------------------------------------------- | Register The Auto Loader |-------------------------------------------------------------------------- | | Composer provides a convenient, automatically generated class loader for | this application. We just need to utilize it! We'll simply require it | into the script here so we don't need to manually load our classes. | */ require '/home/mmpp/project/postal_api/vendor/autoload.php'; project/* |-------------------------------------------------------------------------- | Run The Application |-------------------------------------------------------------------------- | | Once we have the application, we can handle the incoming request using | the application's HTTP kernel. Then, we will send the response back | to this client's browser, allowing them to enjoy our application. | */ $app = require_once '/home/mmpp/project/postal_api/bootstrap/app.php'; $kernel = $app->make(Kernel::class); $response = tap($kernel->handle( $request = Request::capture() ))->send(); $kernel->terminate($request, $response);
3箇所ですね、直接書きます….
はい、これで動きますねw
初めにスタティックで書いていたのですが…. ものすごく数えるのが面倒になりましたw
はい、これで、いろんなアプリが公開できます!