After hours of searching I still could not find my answer regarding 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
$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.
You can pass query string to URL in laravel using named route and controller action. You can pass query string as comma separated array to named route and controller action and redirect to URL.
rs. Path annotation in JAX-RS is used to define a URI matching pattern for incoming HTTP requests. It can be placed upon a class or on one or more Java methods.
Laravel routes are located in the app/Http/routes.A parameter provided in the route is usually annotated with curly braces. For instance, to pass in a name parameter to a route, it would look like this. By convention, the Controller function accepts parameters based on the parameters provided.
To retrieve the query parameters on your Laravel backend, you can make use of either the "Request" class or the "request()" helper method. Imagine you want to get the "search" query from the URL, you can do as follows. $searchQuery = $request->query('search');
Your usage of link_to_route is incorrect:
{!! 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:
{!! 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
A correctly requested route using 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
If you only want to add the title to the URL but not the slug, use a route like this:
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
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
Any added parameter in a route()
call will be added as a GET parameter if it's not existing in the route definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With