0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

laravelのstancl/tenancyで詰まったところ

Last updated at Posted at 2025-02-07

assetのパスについて

なぜか通常のようにassetが使えなかった。
tenancyを入れると標準でassetがtenancy用になっているらしい。
config/tenancy.phpファイルの下記部分が影響しているとのこと。

 'asset_helper_tenancy' => true,
 から
  'asset_helper_tenancy' => false,
 に変更

ルーティングについて

非テナント(central-domain,non tenant)のルーティング(web.php)をいくらいじっても動かなかった。
いつの間にか要らないものを設定していたらしい。
kernel.phpmiddlewereGropのwebを確認

            // \App\Http\Middleware\InitializeTenancyByDomainWithCentralCheck::class,
            // \Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains::class,

RouteServiceProvider.phpにこれが書いてあるかも確認

    public function boot(): void
    {
    何か書いてあるものは消さない
        $this->routes(function () {
        何か書いてあるものは消さない
            $this->mapWebRoutes();<-追記
            $this->mapApiRoutes();<-追記
        });
    }
protected function mapWebRoutes()
{
    foreach ($this->centralDomains() as $domain) {
        Route::middleware('web')
            ->domain($domain)
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }
}

protected function mapApiRoutes()
{
    foreach ($this->centralDomains() as $domain) {
        Route::prefix('api')
            ->domain($domain)
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }
}

protected function centralDomains(): array
{
    return config('tenancy.central_domains', []);
}

リクエスト データ (ヘッダーまたはクエリ パラメータ) に基づいてテナントを識別する方法

これがドキュメント等が全くない
ルーティングファイルに下記を設定しておけば出来てそう(確証はない)

use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;

Route::middleware([
    'api',
    InitializeTenancyByRequestData::class,<-これ
])->group(function () {
    Route::get('/', function () {
        // dd(\App\Models\User::all());
        return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
    });
});

curl -H "X-Tenant: tenantsテーブルにある任意のid" http://自分のドメイン/api
これでちゃんと返っては来ていた

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?