gpt4 book ai didi

php - Laravel:使用不同的参数打印当前路线

转载 作者:行者123 更新时间:2023-12-02 05:16:18 25 4
gpt4 key购买 nike

考虑 Laravel 5 中的以下路由组:

Route::prefix('{locale}')->group(function () {
// ... Other routes here
});

嵌套在这个路由组中的所有路由都有扩展特定布局的 View 。

在这个布局的某个地方,我在我的应用程序中循环了所有可用的语言环境,并放置了链接以更改到这个语言环境。在我看来打印这些链接的最简单方法是什么,保留当前路由和可能的其他路由参数但仅更改 {locale} 参数?

最佳答案

遇到这个问题并想出了这个:

app('url')->toRoute(
request()->route(),
array_merge(request()->route()->parameters(), ['locale' => $otherLocale]),
true
);

你可以把它变成像route这样的助手:

function current_route($params = [], $absolute = true)
{
return app('url')->toRoute(
request()->route(),
array_merge(request()->route()->parameters(), $params),
$absolute
);
}

并用 current_route(['locale' => 'sv']) 调用它

关于php - Laravel:使用不同的参数打印当前路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48067157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com