【发布时间】:2021-09-04 10:14:57
【问题描述】:
我正在使用 Laravel 8。
我使用 route() 助手在 Blade 模板中显示一些链接:
<a href="{{ route('foo', ['bar'=>'baz']) }}">Hello world</a>
结果如下:
<a href="https://example.com/foo?bar=baz">Hello world</a>
但是,我希望将更多查询参数自动添加到 url:
<a href="https://example.com/foo?bar=baz&src=mailing">Hello world</a>
理想情况下,我想使用 Blade 指令激活此功能
<a href="{{ route('foo', ['bar'=>'baz']) }}">Hello world</a>
<a href="{{ route('cool') }}">Cool!</a>
<a href="{{ route('icecream', ['flavour'=>'strawberry']) }}">Strawberry icecream</a>
<hr>
@withqueryparams(['src'=>'mailing'])
{{-- same code here, but the result will differ --}}
<a href="{{ route('foo', ['bar'=>'baz']) }}">Hello world</a>
<a href="{{ route('cool') }}">Cool!</a>
<a href="{{ route('icecream', ['flavour'=>'strawberry']) }}">Strawberry icecream</a>
@withendqueryparams
<a href="https://example.com/foo?bar=baz">Hello world</a>
<a href="https://example.com/cool">Cool!</a>
<a href="https://example.com/icecream/strawberry">Strawberry icecream</a>
<hr>
<a href="https://example.com/foo?bar=baz&src=mailing">Hello world</a>
<a href="https://example.com/cool?src=mailing">Cool!</a>
<a href="https://example.com/icecream/strawberry?src=mailing">Strawberry icecream</a>
我知道如何手动将查询参数添加到路由。 我知道如何定义 Blade 指令。
但是,我不知道如何将查询参数自动添加到解析的路由中。
【问题讨论】:
-
你试过
route('foo', ['bar'=>'baz', 'src'=>'mailing'])吗? -
不幸的是,这并不能解决我的问题。我想避免在
route()的所有调用中指定'src'=>'mailing'。 -
好的。无论如何,你为什么要使用这种风格而不是 /../../.. 默认风格
-
你不能替换里面的内容...你必须使用一个全局函数或类似的一个用户共享
标签: php laravel laravel-blade