繁体   English   中英

Laravel :(如何)可以使用参数链接到命名路由?

[英]Laravel: (How) is it possible to link to a named route with a parameter?

我已经定义了以下路线:

Route::get('/tours/{country_identifier}', ['as' => 'country', 'uses' => 'CountryController@index']);

现在我想使用类似的东西链接到这个命名的路线

route('country')

填写{country_identifier}的参数,例如'/ tours / Canada'。

我怎样才能做到这一点?

您可以将参数作为第二个参数数组传递:

route('country', ['country_identifier' => $someValue]);

你可以看看文档 ,那里有很多有用的例子:)

从视图使用链接(假设您使用刀片)

<a href="{{ URL::route('country', array('country_identifier' => $var)) }}">$var</a>

这很简单:

对于单个链接

<a href="{{ route('country', ['country_identifier' => $country_id])  }}">$country_name</a>

如果您要生成一些链接,您可以这样做

@foreach($countries as $country)
<a href="{{ route('country', ['country_identifier' => $country->id])  }}">$country->name</a>
@endforeach

注意: $countries是传递给模型对象的视图示例$countries = Country::all()

尝试这个

route('country', array('country_identifier' => 'Canada')) 

路线需要'canada'参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM