Laravel:(如何)是否可以使用参数链接到命名路由?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30503989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: (How) is it possible to link to a named route with a parameter?
提问by Pim
I have defined the following route:
我定义了以下路线:
Route::get('/tours/{country_identifier}', ['as' => 'country', 'uses' => 'CountryController@index']);
Now I would like to link to this named route using something like
现在我想使用类似的东西链接到这个命名路线
route('country')
with a parameter for {country_identifier} filled in, e.g. '/tours/Canada'.
填写了 {country_identifier} 的参数,例如“/tours/Canada”。
How can I make this happen?
我怎样才能做到这一点?
采纳答案by Sh1d0w
You can pass the parameters as second argument array:
您可以将参数作为第二个参数数组传递:
route('country', ['country_identifier' => $someValue]);
You can take a look at the documentation, there are a lot of useful examples there :)
你可以看看文档,那里有很多有用的例子:)
回答by Szenis
To link from a view use (assuming you use blade)
从视图使用链接(假设您使用刀片)
<a href="{{ URL::route('country', array('country_identifier' => $var)) }}">$var</a>
回答by Emeka Mbah
Its simple:
这很简单:
For a single link
对于单个链接
<a href="{{ route('country', ['country_identifier' => $country_id]) }}">$country_name</a>
If you are generating some links you can do for example
如果你正在生成一些链接,你可以做例如
@foreach($countries as $country)
<a href="{{ route('country', ['country_identifier' => $country->id]) }}">$country->name</a>
@endforeach
NB: Where $countries
is model object passed to view example $countries = Country::all()
注意:$countries
模型对象在哪里传递给视图示例$countries = Country::all()
回答by Ashwini G.
Try this
尝试这个
route('country', array('country_identifier' => 'Canada'))
It will take 'canada'as parameter to route.
它将以 'canada' 作为参数进行路由。