Route parameters with named route

Published 2 months ago by Dario

I'm using named route all over my L5.3 project and for a named route like the one below I got a strange behavior. Say we have this:

Route::post('foo/bar/{idy}', [email protected]')->name('foo.bar');
route('foo.bar', ['idx' => 1]);

This results in : http://domain.com/foo/bar/1

While I would have this: http://domain.com/foo/bar/?idx=1

Please, is there anyone of you with a kind explanation? This is the basic behavior:

Route::get('user/{id}/profile', function ($id) {
    //
})->name('profile');

$url = route('profile', ['id' => 1]);
Best Answer (As Selected By Dario)
Snapey

Tests with tinker;

// if there is only one parameter it just uses that even though the name is not a ,match;
>>> route('foo.bar',['idx'=>3])
=> "http://localhost/foo/bar/3"

// if there are two parameters then they are matched correctly
>>> route('foo.bar',['idy'=>1,'idx'=>3])
=> "http://localhost/foo/bar/1?idx=3"

// irrespective of the order
>>> route('foo.bar',['idx'=>3,'idy'=>1])
=> "http://localhost/foo/bar/1?idx=3"

// passing null as the first argument gives the desired output
>>> route('foo.bar',[null,'idx'=>3])
=> "http://localhost/foo/bar?idx=3"

// or if it is named
>>> route('foo.bar',['idy'=>null,'idx'=>3])
=> "http://localhost/foo/bar?idx=3"

// but only if it is the first argument
>>> route('foo.bar',['idx'=>3, 'idy'=>null])
=> "http://localhost/foo/bar/3?"
mikefolsom

Are you actually wanting query parameters in a POST route?

EventFellows

what is your question?

a route parameter and a query string are two fundamentally different things...

Snapey
Snapey
2 months ago (577,655 XP)

You are not making idy optional in the route. What do you expect there if it is not optional?

try

Route::post('foo/bar/{idy?}', [email protected]')->name('foo.bar');
Dario

The result of the route() I've shown above is unexpected to me. I would have expected the not matching parameter to become part of the querystring as shown.

Route::post('foo/bar/{idy?}', [email protected]')->name('foo.bar');
route('foo.bar', ['idx' => 1]);

I would have http://domain.com/foo/bar/?idx=1 instead I got http://domain.com/foo/bar/1

I'm sorry but I don't know how to explain it better than this.

Snapey
Snapey
2 months ago (577,655 XP)

So the question mark made no difference then?

Dario

Nop. I had just forgotten to add it to the sample code. The problem here is that I would know if there could be a workaround that anybody has already faced. As far as I know, this is a standard behavior but to me it's not the best one approach. Laravel should be enhanced to allow only matched keys to be on the url segments and the others to be part of the querystring.

Snapey
Snapey
2 months ago (577,655 XP)

Nop. I had just forgotten to add it to the sample code.

It is quite important to save wasting peoples time...

Snapey
Snapey
2 months ago (577,655 XP)

Tests with tinker;

// if there is only one parameter it just uses that even though the name is not a ,match;
>>> route('foo.bar',['idx'=>3])
=> "http://localhost/foo/bar/3"

// if there are two parameters then they are matched correctly
>>> route('foo.bar',['idy'=>1,'idx'=>3])
=> "http://localhost/foo/bar/1?idx=3"

// irrespective of the order
>>> route('foo.bar',['idx'=>3,'idy'=>1])
=> "http://localhost/foo/bar/1?idx=3"

// passing null as the first argument gives the desired output
>>> route('foo.bar',[null,'idx'=>3])
=> "http://localhost/foo/bar?idx=3"

// or if it is named
>>> route('foo.bar',['idy'=>null,'idx'=>3])
=> "http://localhost/foo/bar?idx=3"

// but only if it is the first argument
>>> route('foo.bar',['idx'=>3, 'idy'=>null])
=> "http://localhost/foo/bar/3?"
Dario

Snapey, thanks indeed for your suggestion. Memento is my second name of course. Tinker is the saver and I often forget to use it! By the way, help the others is never a waste of time. If so, don't do it.

Cronix
Cronix
2 months ago (132,490 XP)

@Dario I think he meant omitting crucial pieces of info wastes peoples time who are trying to help because they don't have all of the relevant info, not helping in general. If you notice, @snapey helps quite a bit around here.

Dario

@Cronix, you read my words omitting crucial pieces. @Snapey, by the way, helped a lot indeed and I've explained why and what. Finally I've given a suggestion about considering what can be a waste of time and what not in order to save time in the future. In general, I never consider waste of time helping whatever it would be the end result. I consider it experience! By the way, Thanks guys both.

Sign In or create a forum account to participate in this discussion.