簡體   English   中英

多種實體類型的 Laravel 子域路由

[英]Laravel subdomain routes for multiple entity types

我有一個有多個子域的項目。 例如,我有一個學生的子域,它轉到學生控制器,它看起來像這樣:

Route::domain('students.domain.test')->group(function () {
    Route::get('/', function () {
        return "done reaching the students page";
    });
});

第二種類型的域是“domain.test”和我在請求級別檢查的任何子域,這也很好。

Route::get('/', [HomeController::class, 'index'])->name('index');

但是在第二種類型的域之前,我想為數據庫中的特定類型的實體創建子域。

Route::domain('{someTypes}.domain.test')
->group(function () {
    Route::get('/', function () {
        return "done reaching SomeTypes Page";
    });
});

我的實體表具有以下屬性:Id、Title、Type“我想檢查類型是否為 5”。

我嘗試使用中間件:

    public function handle($request, Closure $next, ...$types)
{
    $currentEntity = app('current_entity');
    if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
        return $next($request);
    }
    abort(404, 'Sorry, Request Not Found');
}

我像這樣將它應用到我的路線上:

Route::group([
'middleware' => ['type:journal']
],function () {
    Route::get('/', function(){
        return 'journals logic goes here';
    });

});

我還有另一個中間件可以忽略這樣的類型:

    public function handle($request, Closure $next, ...$types)
{
    $currentEntity = app('current_entity');
    if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
        abort(404, 'Sorry, Request Not Found');
    }
    return $next($request);
}

並將其應用於其他路線,如下所示:

Route::group([
'middleware' => ['except_entity:journal']
], function(){
    Route::get('/', function(){
    return 'default pages when journals fails';
})->name('index');

我希望它清楚我想要實現的目標。

首先,您需要檢查一下您使用的是哪個版本的 Laravel?

您需要使用中間件。 而且我認為,使用 laravel 6、7 或 8 進行編碼的方法有點不同。

您能否向我們提供有關您的代碼的更多信息,以便我們可以更輕松地提供幫助?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM