laravel 既存サイトにlaravelを追加

特定のフォルダ内のみlaravelで表示させたい

作成日:2024-03-18, 更新日:2024-03-19

2024-03-19時点でのメモ(laravel v10.46.0)

このページのやり方だとダメかも

  • ブラウザで開くと「Uncaught RuntimeException: A facade root has not been set.」というエラーになる
  • 「bootstrap/app.php」に「$app->bootstrapWith()」の設定をするとエラー解消
  • でも「php artisan route:list」を実行すると「Illuminate\Routing\UrlGenerator::__construct(): Argument #2 ($request) must be of type Illuminate\Http\Request, null given, called」というエラーになる
  • 「php artisan route:list」は気にしなくても良い…という話でもあるけどroute(xxx)でエラーになるものとならないものがある(※「bootstrap/app.php」に追記した「$app->bootstrapWith()」を削除すれば解決)→他にどのようなエラーが出てくるかは今後のお楽しみ…となる

別のやりかた→「laravel 既存サイトにlaravelを追加(.htaccess)

やりたいこと

既存サイトがある…特定のフォルダへのアクセスをlaravelで対応・表示させたい

既存サイトの構成

  • /public
  • /public/css
  • /public/js
  • /public/index.php
  • /public/about.html

laravel追加後の構成

  • /public
  • /public/css
  • /public/js
  • /public/index.php
  • /public/about.html
  • /public/contents1 ← 追加
  • /public/contents2 ← 追加
  • /app/xxx ← 追加
  • /routes/web_contents1.php ← 追加
  • /routes/web_contents2.php ← 追加
  • /bootstrap/app.php ← 追加
  • 他、laravelのファイルたち

「public/index.php」がすでにいるのでlaravelで使えない…という問題がある
.htaccessもすでに存在する場合は…その.htaccessも正直触りたくない…という問題もある

手順

  1. ルーティングファイルの準備
  2. 「public/contents1」「public/contents2」を追加
  3. RouteServiceProviderに設定追加
  4. bootstrap/app.phpに設定追加
  5. ルーティングファイルの設定

ルーティングファイルの準備

「routes/web_contents1.php」を用意
「contents1/hoge」へのアクセスのルーティング設定は「Route::get('contents1/hoge', [Controllers\HogeController::class, 'index']);」ではなく「Route::get('hoge', [Controllers\HogeController::class, 'index']);」となる

「public/contents1」「public/contents2」を追加

laravel内の「public/index.php」「public/.htaccess」を「public/contents1」「public/contents2」内に複製

「public/contents1」の各PATHを変更して「require __DIR__.'/../vendor/autoload.php';」を「require __DIR__.'/../../vendor/autoload.php';」にする。他同様。
web_contents1.phpを読み込ませる設定も必要

contents1/index.php

// 省略
$app = require_once __DIR__.'/../../bootstrap/app.php'; // 他の各PATH同様

// ▼追加: web_contents1.phpを読み込ませる設定
$app->router->group([], function ($router) {
    require __DIR__.'/../../routes/web_contents1.php';
});

// 省略

※「contents2/index.php」も同様

contents1/.htaccess

# 省略
RewriteEngine On
RewriteBase /contents1/ # ←コイツが必要
# 省略

※「contents2/.htaccess」も同様

RouteServiceProviderに設定追加

// 省略
public function boot(): void
{
    // 省略

    $this->routes(function () {
        // 省略

        Route::middleware('web') // ← ココの設定方法が微妙…
            ->group(base_path('routes/web_contents1.php'));
    });
}

bootstrap/app.phpに設定追加

// 省略
$app->bootstrapWith([
    'Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables',
    'Illuminate\Foundation\Bootstrap\LoadConfiguration',
    'Illuminate\Foundation\Bootstrap\HandleExceptions',
    'Illuminate\Foundation\Bootstrap\RegisterFacades',
    'Illuminate\Foundation\Bootstrap\RegisterProviders',
    'Illuminate\Foundation\Bootstrap\BootProviders',
]);

return $app;

ルーティングファイルの設定

// 省略
Route::group(['middleware' => 'web'], function () { // ['middleware' => 'web']が必要
    Route::prefix('login')->group(function(){
// 省略

ミドルウェアグループを「web」じゃなく「web_contents1」にしたい

  • RouteServiceProviderに設定
  • app/Http/Kernel.phpに設定
  • ルーティングファイルの設定

RouteServiceProviderに設定追加

// 省略
public function boot(): void
{
    // 省略

    $this->routes(function () {
        // 省略

        Route::middleware('web_contents1') // ← これがいるのか微妙…
            ->group(base_path('routes/web_contents1.php'));
    });
}

本来はmapメソッドで設定すれば良いらしい。設定するとルーティングファイルの設定から「['middleware' => 'web_contents1']」の記述が省略できるらしい

app/Http/Kernel.phpに設定

// 省略
    protected $middlewareGroups = [
        // 省略

        'web_mypage' => [ // 今回は、webのほうをコピペしただけ。必要に応じて修正
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        // 省略

ルーティングファイルの設定

// 省略
Route::group(['middleware' => 'web_contents1'], function () {
    Route::prefix('login')->group(function(){
// 省略