在 Laravel Blade 模板中链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20916952/
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
Linking in Laravel Blade Template
提问by patrick
I am quite new to Laravel but I've watched quite a lot of tutorials to get into it, so I am used to it now. However, I am stuck at a point where I want to link to a users favorites in one of my blade.php files.
我是 Laravel 的新手,但我已经看了很多教程来入门,所以我现在已经习惯了。但是,我一直想在我的blade.php 文件之一中链接到用户的收藏夹。
This is the link in my bootstrap navbar:
这是我的引导程序导航栏中的链接:
<li><a href="{{ URL::to('users/' . $user->id . '/favortites' }}"><i class="fa fa-heart"> </i> Favorites</a></li>
I have tried using URL::to but it does not work. I am not sure whether I am implementing the userId correctly.
我曾尝试使用 URL::to 但它不起作用。我不确定我是否正确实现了 userId。
In the end the link I want to access should look like this: users/5/favorites if the user with an id of 5 is the authenticated user.
最后,我想访问的链接应该是这样的:如果 id 为 5 的用户是经过身份验证的用户,则 users/5/favorites。
I would really appreciate some help here.
我真的很感激这里的一些帮助。
Thank you.
谢谢你。
回答by albur
There are several options. You can use the link_to_action
/ HTML::linkAction
helper if you're linking to a controller's method:
有几种选择。如果您要链接到控制器的方法,则可以使用link_to_action
/HTML::linkAction
助手:
<li>{{ HTML::linkAction('UserController@getFavorites', 'Favorites', ['id' => 5], ['class' => 'abc']) }}</li>
Another option is to link to a named route with link_to_route
/ HTML::linkRoute
:
另一种选择是使用link_to_route
/链接到命名路由HTML::linkRoute
:
<li>{{ HTML::linkRoute('user.favorites', 'Favorites', ['id' => 5], ['class' => 'abc']) }}</li>
You can find more information on helpers in the documentationand in the API pages for HtmlBuilderand UrlGenerator.
您可以在文档以及HtmlBuilder和UrlGenerator的 API 页面中找到有关帮助程序的更多信息。
回答by user3213254
HTML::linkAction /
HTML::linkAction /
have a look at the docs for more :)
查看更多文档:)
laravel helpers
Laravel 助手