laravel 刀片创建 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42270100/
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 blade creating url
提问by Przemek
I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
我有一个简单的问题,基本上我是从数据库中获取网站名称并根据它的名称创建一个链接。看起来像:
@foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a> @endforeach
Which gives for example: http://localhost/name
例如: http://localhost/name
Howver links needs to be like this:
但是链接需要是这样的:
http://localhost/website/name
how can I add /website
into my URL using blade template in laravel?
http://localhost/website/name
如何/website
在 Laravel 中使用刀片模板添加到我的 URL 中?
回答by Filip Sobol
Try this:
尝试这个:
{{ url('website/' . $website->name) }}
回答by motia
This have some improvement on @Laran answer regarding best practices.
这对@Laran 关于最佳实践的回答有一些改进。
You would better use url parameters instead of concatenating the $name parameter
您最好使用 url 参数而不是连接 $name 参数
{{ url('website', [$name]) }}
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
并且使用命名路由将更好地将路由与视图分离。
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}
并写在你的 {{ route('website', [$name]) }}