在刀片模板中使用命名URL

编程入门 行业动态 更新时间:2024-10-24 20:17:40
本文介绍了在刀片模板中使用命名URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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?

我遇到了这个问题: laravel/docs/5.1/helpers ,但这只是一张白纸,没有实际使用方法的相关内容.

I came across this: laravel/docs/5.1/helpers, but it was just a piece of a white page without relevant content of how to actually use it.

推荐答案

您可以使用记录在route()帮助器. rel ="noreferrer">此处.

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

本文发布于:2023-10-23 06:05:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:刀片   模板   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!