Laravel 5 - link_to_route() 方法通过添加“?”使我的路由参数更改为查询字符串 在末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30587262/
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 5 - link_to_route() method making my route parameters to change into Query String by adding "?" at the end
提问by Saj
After hours of searching I still could not find my answer regarding L5
.
经过数小时的搜索,我仍然找不到关于L5
.
What my issue is :
我的问题是:
I want to make a link something like this:
我想建立一个这样的链接:
localhost:800/songs/you-drive-me-crazy
BUT what is get is:
但是得到的是:
localhost:800/songs?you-drive-me-crazy
my route parameter is changing into query string.
我的路由参数正在更改为查询字符串。
//routes.php
//路由.php
$router->bind('songs', function($slug)
{
return App\Song::where('slug', $slug)->first();
});
$router->get('songs', ['as' => 'songs.index', 'uses' => 'SongsController@index'] );
$router->get('songs/{songs}', ['as' => 'songs.show', 'uses' => 'SongsController@show'] );
I am using:
我在用:
{!! link_to_route('songs.index', $song->title, [$song->slug]) !!}
I have tried everything but not succeeded yet,your suggestion may be helpful.
我已经尝试了所有方法但尚未成功,您的建议可能会有所帮助。
Thanks.
谢谢。
回答by Luceos
Your usage of link_to_route is incorrect:
您对 link_to_route 的使用不正确:
{!! link_to_route('songs.index', [$song->title, $song->slug]) !!}
The first parameter is the route name, the second parameter is an array of route parameters, preferably using key value. Because you did not show your defined route, it's hard to guess what this associative array should look like:
第一个参数是路由名称,第二个参数是路由参数数组,最好使用key值。因为您没有显示您定义的路线,所以很难猜测这个关联数组应该是什么样子:
{!! link_to_route('songs.index', ['title'=>$song->title, 'slug'=>$song->slug]) !!}
Also I advise you to use the documented functions: route()
, see: http://laravel.com/docs/5.0/helpers#urls
此外,我建议您使用记录在案的功能:route()
,请参阅:http: //laravel.com/docs/5.0/helpers#urls
A correctly requested route using route()
:
正确请求的路线使用route()
:
{!! route('songs.index', ['title'=>$song->title, 'slug'=>$song->slug]) !!}
A properly formatted route would then be:
格式正确的路由将是:
Route::get('songs/{title}/{slug}', ['as' => 'songs.index', 'uses' => 'SomeController@index']);
This will result in a URL like: http://localhost:800/songs/you-drive-me-crazy/slug
这将导致如下 URL: http://localhost:800/songs/you-drive-me-crazy/slug
If you only want to add the title to the URL but not the slug, use a route like this:
如果您只想将标题添加到 URL 而不是 slug,请使用如下路由:
Route::get('songs/{title}', ['as' => 'songs.index', 'uses' => 'SomeController@index']);
This will result in a URL like: http://localhost:800/songs/you-drive-me-crazy/?slug=slug
这将导致如下 URL: http://localhost:800/songs/you-drive-me-crazy/?slug=slug
Using
使用
Route::get('songs/{slug}', ['as' => 'songs.index', 'uses' => 'SomeController@index']);
The URL will be like: http://localhost:800/songs/you-drive-me-crazy/?title=title
assuming the slug now is you-drive-me-crazy
该 URL 将类似于:http://localhost:800/songs/you-drive-me-crazy/?title=title
假设 slug 现在是you-drive-me-crazy
Any added parameter in a route()
call will be added as a GET parameter if it's not existing in the route definition.
route()
如果路由定义中不存在,则调用中添加的任何参数都将作为 GET 参数添加。
回答by Saj
fixed it, thanks for your great concerns and suggestions.
已修复,感谢您的极大关注和建议。
I was linking to wrong route here:
我在这里链接到错误的路线:
`{!! link_to_route('songs.index', $song->title, [$song->slug]) !!}`
now, I changed it as :
现在,我将其更改为:
`{!! link_to_route('songs.show', $song->title, [$song->slug]) !!}`
and it did the trick.
它做到了。