public
のディレクトリをドキュメントルートとした場合、ドキュメントルートよりも上のディレクトリにapp
, bootstrap
, config
といったディレクトリが展開されることとなります。
ドキュメントルートの上のディレクトリにはアクセスログなどのディレクトリがあり、それらのディレクトリと混ざってしまうことがあるかと思います。
Laravelのファイルはディレクトリを追加して、そこに格納するようにしたいと思います。
laravel/frameworkのバージョンはv9.45.1
です。
目的とするディレクトリ構造
laravel
というディレクトリにソースを格納し、/var/www/public/
をドキュメントルートとするディレクトリ構造を目指します。
/var/www/ ├ logs/ <もともとあるディレクトリなど> │ ├ ... │ └ access-yymmdd.log ├ laravel/ <Larabelのソースを格納するディレクトリ> │ ├ app/ │ ├ bootstrap/ │ ├ lang/ │ ├ node_modules/ │ ├ resources/ │ ├ routes/ │ ├ storage/ │ ├ tests/ │ ├ vendor/ │ ├ .editorconfig │ ├ .env │ ├ .gitattributes │ ├ .gitignore │ ├ artisan │ ├ composer.json │ ├ composer.lock │ ├ manifest.json │ ├ package-lock.json │ ├ package.json │ ├ phpunit.xml │ ├ README.md │ └ vite.config.js └ public/ <DocumentRoot> ├ build/ ├ .htaccess ├ favicon.ico ├ index.php └ robots.txt
作業手順
手順といってもファイルの移動、3ファイルの更新で完了します。
ファイルの移動
laravel
のディレクトリを作成し、public
のディレクトリ以外のディレクトリとファイルをlaravel
のディレクトリに移動します。
public/index.phpの書き換え
public/index.php
に記載のあるパス(3か所)にlaravel/
を追記します。
if (file_exists($maintenance = __DIR__.'/../laravel/storage/framework/maintenance.php')) { require $maintenance; }
require __DIR__.'/../laravel/vendor/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
AppServiceProvider.phpの書き換え
app/Providers/AppServiceProvider.php
のregister
関数に追記します。
/** * Register any application services. * * @return void */ public function register() { $this->app->bind('path.public', function () { return base_path() . '/../public'; }); }
Viteの変更
vite.config.js
のlaravel-vite-pluginにpublicDirectory
を追加します。
export default defineConfig({ plugins: [ laravel({ publicDirectory: '../public', input: ['resources/css/app.css', 'resources/js/app.js'], refresh: true, }), ], });