Laravel 路由:在单个控制器方法中处理子域和主域的参数

问题描述 投票:0回答:1

有两组路由:子域和主域:

Route::middleware(['language'])->group(function () {
    Route::domain('{subdomain}.vizit.loc')->group(function () {
        Route::controller(FrontSightController::class)->group(function () {
            Route::get('/sights', 'index')->name('subdomain.main.sights.index');
            Route::get('/sights/category/{category_slug}', 'showByCategory')->name('subdomain.main.sights.category.index');
        });
    });
});

Route::middleware(['language'])->group(function () {
    Route::domain('vizit.loc')->group(function () 
        Route::controller(FrontSightController::class)->group(function () {
            Route::get('/sights', 'index')->name('main.sights.index');
            Route::get('/sights/category/{category_slug}', 'showByCategory')->name('main.sights.category.index');
        });
    });
});

在索引方法中,我传递了 $subdomain 参数,并且两个组的一切都工作正常:

public function index(Request $request, $subdomain = null)

但是,在showByCategory方法中,多了一个$category_slug参数:

public function showByCategory(Request $request, $category_slug, $subdomain = null)
    {
        $subdomain = $request->route('subdomain');
        $region_id = $this->regionService->getRegionIdByDomain($subdomain);
        $categories = Category::whereNull('category_id')->get(['id', 'name', 'slug']);
        $regions = Region::with('districts')->get(['id', 'name', 'slug']);
        $services = Service::get(['id', 'name', 'slug', 'icon']);
        $category_id = Category::where('slug', $category_slug)->value('id');
        $sights = Sight::where('category_id', $category_id)->filter($request)->where('region_id', $region_id)->latest()->paginate(20);
        dd($subdomain, $region_id, $category_slug, $sights);
        if (is_null($region_id)) {
            $sights = Sight::where('category_id', $category_id)->filter($request)->latest()->paginate(20);
        }
        return view('main.sights.index', compact('sights', 'categories', 'regions', 'services'));
    }

当我单击子域的链接时,该方法行为不正确,因为 $subdomain 和 $category_slug 的值在刀片模板中相同:

@if(!is_null($subdomain))
                        <a href="{{ route('subdomain.main.sights.category.index', [$subdomain, $sight->category->slug]) }}" class="btn btn-outline-weldonBlue">
                            {{ $sight->category->name[app()->getLocale()] }}
                        </a>
                    @else
                        <a href="{{ route('main.sights.category.index', $sight->category->slug) }}" class="btn btn-outline-weldonBlue">
                            {{ $sight->category->name[app()->getLocale()] }}
                        </a>
                    @endif

如果我在 showByCategory 方法中将 $subdomain 和 $category_slug 设为可选参数,则主域的链接将不起作用。

在 Route::domain('{subdomain}.vizit.loc') 设置中,{subdomain} 不能是可选参数。

有没有办法解决这个问题,而不需要对子域和主域重复相同的方法?

php laravel subdomain
1个回答
0
投票

也许这会有所帮助,在 Laravel 文档中关于 subdomain 的内容。

$subdomain
位于
$id

之前

但是在你的

showByCategory
中,你把
$subdomain
放在了后面。这将使 Laravel 将
{subdomain}
的值放入您的
$category_slug
中。

此外,我可以看到你设置

$subdomain = $request->route('subdomain');

可能是因为在调试时,您看到

$subdomain
的值不正确。此行会将
{subdomain}
分配给您的
$subdomain
,它意外地与
$category_slug
相同。尝试交换
$subdomain
$category_slug
位置。

希望这个建议能帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.