在刀片模板中使用命名URL [英] Using Named URL in blade template
问题描述
在Django中,我可以这样做:
In Django, I can do this:
<a href="{% url 'account_login' %}">Account Link</a>
这将给我domain/account/login
在我的urls.py中命名该URL的位置
which would give me domain/account/login
where I have that URL named in my urls.py
url(r'^account/login/$', views.Login.as_view(), name='account_login'),
我想在Laravel 5.2中做类似的事情
I want to do something similar in Laravel 5.2
我目前有这样的东西:
Route::get('/survey/new', ['as' => 'new.survey', 'uses' => 'SurveyController@new_survey']);
如何在模板中使用以及传递参数?
How do I use in my template, plus passing in parameters?
我遇到了这个问题: https://laravel.com/docs/5.1/helpers ,但这只是一张白纸,没有实际使用方法的相关内容.
I came across this: https://laravel.com/docs/5.1/helpers, but it was just a piece of a white page without relevant content of how to actually use it.
推荐答案
您可以使用记录在
You can use the route()
helper, documented here.
<a href="{{ route('new.survey') }}">My Link</a>
如果包含laravelcollective/html
,则可以使用link_to_route()
.它最初是核心的一部分,但已在Laravel 5中删除.在此处
If you include laravelcollective/html
you could use link_to_route()
. This was originally part of the core but removed in Laravel 5. It's explained here
{!! link_to_route('new.survey', 'My Link') !!}
Laravel集体在此处中记录了上述帮助器.函数原型如下
The Laravel Collective have documented the aforementioned helper here. The function prototype is as follows
link_to_route($routeName, $title = null, $parameters = [], $attributes = []);
例如,如果您想使用参数,则它接受键值对的数组,这些键值对对应于路由URI中的命名段.
If for example you wanted to use parameters, it accepts an array of key value pairs which correspond to the named segments in your route URI.
例如,如果您有路线
Route::get('surveys/{id}', 'SurveyController@details')->name('detail.survey');
您可以使用参数中的以下内容来生成此路线的链接.
You can generate a link to this route using the following in the parameters.
['id' => $id]
完整的示例,回显标记,其中包含指向指定路线的锚点.
A full example, echoing markup containing an anchor to the named route.
{!! link_to_route('new.survey', 'My Link', ['id' => $id]) !!}
这篇关于在刀片模板中使用命名URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!