t0berius's avatar

laravel redirect to route with GET parameter

How to redirect to a route and include a parameter as GET parameter into this route?

return redirect()->route('postSearch', ['q' => $request->search]);

I'm looking for something like:

/route?q=xyz

Instead at the moment it's returning route model binding "syntax" like:

/route/xyz

Is there any parameter to use inside the route() method?

0 likes
17 replies
jlrdw's avatar

You can or could build a redirect with session params:

return redirect('dog/indexadmin?p=' . Session::get('dogpage') . '&psch=' . Session::get('dogsearch') . '&aval=' . Session::get('dogaval'));

Could also store all all that in a session variable somewhere and:

$vurl = Session::get('areturn');
return redirect($vurl);
//or
 return redirect(Session::get('areturn'));
Snapey's avatar

I think if your route accepts a parameter then it gets added into the path, otherwise its added to the querystring

what I mean is

Route::get('route/{q}', 'SearchController@search')->name('postSearch');

versus

Route::get('route', 'SearchController@search')->name('postSearch');

after testing, yes, it tries to satisfy the placeholders in the route first and then anything left over is added as querystring

1 like
jlrdw's avatar

If using query string, those parameters don't need to be put in the route, they are automatically in the request object.

Route::get('route', 'SearchController@search')->name('postSearch');

If the QS was

site.com?page=1&something=tree$name=bob
page=1&something=tree$name=bob  // are handled automatically

You get them through request, they are not route parameters.

Example in the controller method:

$currentname = Request::input('name');

echo $currentname;

// Would echo bob
jlrdw's avatar

@SNAPEY - Yes a redirect also works with the query string parameters with no problem.

I do this often:

return redirect('dog/indexadmin?p=' . Session::get('dogpage') . '&psch=' . Session::get('dogsearch') . '&aval=' . Session::get('dogaval'));

After an edit operation, it returns me to the page I was on.

You have to set that stuff in session prior to using.

Example the

Session::get('dogpage')  // take that part for example

Controller:

$page = Request::input('page', '1');

Will also get the correct page from a redirect. Try it and see url, it's exactly where you said to redirect to.

However when using session to build up a query string or a redirect, you have to include the page also, how else will the redirect know what page you are wanting to go to.

If using the redirect that way.

Edit:

Note also if part of URL is pretty url:

site.com/param1/param2?page=1&something=tree$name=bob

That works also, just need a route setup to handle param1/param2

So:

Route::get('route/{param1}/{param2}', 'SearchController@search')->name('postSearch');

The query string part is not needed in the route, query string is passed anyway.

This part is query string:

?page=1&something=tree$name=bob

This part

/param1/param2

Are passed parameters.

They are all parameters, but the query string you don't have to worry about passing in a route, it's an auto HTTP or HTTPS thing.

Snapey's avatar

@jlrdw you're off on one again...

What has session got to do with the question. All the OP wants to know is how to construct a URL to be used in a redirect.

I did not read half of your ramblings because a) I don't need to know how sessions hold data and b) its got nowt to do with the question

1 like
jlrdw's avatar

@SNAPEY - First I was also referring to https://laracasts.com/discuss/channels/laravel/return-view-with-query

If OP is using session, and needs to redirect with those parameters, that's how to do it.

Second, it was not ramblings. Even if I misunderstood something in question, that is still the way session, query string, redirect, and a route would work. So the post could still help someone.

The question was:

How to redirect to a route and include a parameter as GET parameter into this route?

I answered, YOU DO NOT NEED A GET PARAMETER IN A ROUTE. It's automatically passed.

@snapey surely you should know that a route does not need a get parameter or post parameters, they are already taken care of.

Now what else.

include a parameter as GET parameter into this route?

You don't. GET parameters are query string, others they are just passed parameters.

param1 /param2/param3 is not query string.

Besides, instead of cutting me down, you could have offered a solution.

What I can't imagine, there is only so many ways to deal with query string. It's just automatic, there when you use it. Almost no thought involved its part of http request.

Instead of working together to assist OP, now OP might be confused. Even if I was totally wrong, it would have been better if you gave a reply, they selected yours and moved on. We are suppose to be helping, which is what I was trying to do.

I just thought that OP was thinking that a query string has to be in a route, but it don't.

I have seen other question from OP, and happen to know that they normally use regular route parameters.

So please let's not argue, let's help.

@jaheller I am sorry if I misunderstood anything in your post.

1 like
Cronix's avatar

@snapey had the correct answer in his first post.

If route is

Route::get('route', 'SearchController@search')->name('postSearch');

and in the controller you redirected to that named route, and passing parameters, they will appear in the query string.

return redirect()->route('postSearch', ['q' => 4]);

then it would redirect to /route?q=4 because there is no {q} placeholder in the route definition, so it gets added to the query string

If the route was defined as

Route::get('route/{q}', 'SearchController@search')->name('postSearch');

then that same redirect would go to /route/4 as a url parameter, because {q} is a named placeholder.

Also, yes the question was

How to redirect to a route and include a parameter as GET parameter into this route?

That would be using redirect()->route().

6 likes
jlrdw's avatar

But the point is you do not need {q} In a route if it's query string.

A regular redirect will find that route.

Now you would probably need that route keyword if dealing with regular parameters also.

Route::get('route', 'SearchController@search')->name('postSearch');

If that route was called it would automatically pick up on the query string since it is passed automatically.

so why mess with putting a {q} in a route when it's never needed if it's a query string.

Why mess with redirecting to a route when it's totally unnecessary just do a regular redirect with the query String Attached, the correct route will just work.

In fact the only time there's any confusion is the passing of parameters. Meaning route parameters not query string parameters.

But I guess either way works, but to put something in there that's totally not needed I apologize but to me it doesn't make sense.

This would be making the OP believe that no matter what framework they have to also pass a query string as a route parameter which is totally 180 degrees from how the query string works.

I guess keep it simple is a thing of the past.

@cronix are you telling me that when you paginate you somehow put that query string part for pagination in the route.

Something tells me you don't.

Cronix's avatar

But the point is you do not need {q} In a route if it's query string.

Yes, that was pointed out by Snappey's post, and I clarified.

A regular redirect will find that route.

Look at the OP's post, talking about redirect()->route() and using a route name to redirect to it. It's much cleaner/simpler. Named routes are nice. You should learn them. It would clean up all of the code you posted instead of manually building up query strings like it was 2005.

Cronix's avatar

But I guess either way works, but to put something in there that's totally not needed I apologize but to me it doesn't make sense.

No one was saying to do that though. We were explaining the difference between if there is a named route parameter, and if there isn't. I gave clear examples on the differences, and Snapey explained them as well. I'm really not sure what you're going on about.

jlrdw's avatar

@CRONIX - I said the exact same, but to use a regular redirect:

If this was a url

site.com/param1/param2?a=1&b=2&c=3

I said you have to account in the route for param1/param2

Okay I think up to here that's true.

I also said basically you would need:

Route::get('route/{param1}/{param2}', 'SearchController@search')->name('postSearch');

But you do nothing with the

?a=1&b=2&c=3

It is auto passed.

So if using only query string no place holder is needed. If using one or two parameters (not the query string) yes place holders are needed.

I basically said the same thing except gave more examples.

So a redirect for this:

site.com/param1/param2?a=1&b=2&c=3

Could look like:

return redirect('site.com/$param1/$param2?a=' . Session::get('a1') . '&b=' . Session::get('b1') . '&c=' . Session::get('c1'));

Would work and is the easiest.

That is if and only if OP is dealing with some regular parameters and the rest query string.

But at this point, this post has a complete tutorial on the difference between parameters and query string at least.

To note this sentence:

So if using only query string no place holder is needed. If using one or two parameters (not the query string) yes place holders are needed.

Is all I am trying to say.

Now isn't that also what snapey said, I just put in more detail.

Not trying to argue, I think OP is so used of regular parameters that they thought they were needed for query string as well.

Snapey's avatar

@jaheller In case you missed it in all that BS above

it tries to satisfy the placeholders in the route first and then anything left over is added as querystring

jlrdw's avatar

Okay I had this:

?page=' . Session::get('dogpage') . '&psch=' . Session::get('dogsearch') . '&aval=' . Session::get('dogaval')

Stored in a variable named:

$vurl

Okay I tried named routes.

What is the difference having

return redirect()->route('dogadmin', $vurl);

and

return redirect()->route('dogadmin', ['psch' =>Session::get('dogsearch'), 'dogsearch => Session::get('dogaval')]);
  • First, you still have to "build the url"

  • either of the above works.

So? Why the fuss when

return redirect()->route('whatever', $vurl);

And $vurl can be a url string or an array as cronix explained, doesn't matter which, Taylor has "magic in that vendor folder to figure out which one.

To recap

return redirect()->route('your_route', Array OR Query string here, Taylor knew what to do.);
Snapey's avatar

@jldrw keep digging buddy. still missing the point. Go back and read the early posts.

jlrdw's avatar

I'm not digging, I followed cronix suggestion and tried out a named route.

Was curious if it had to be a passed array.

Discovered either way works.

Please, why are you over thinking simple stuff. Taylor made the framework highly flexible, and I was just pointing out the the named route would accept either technique.

You said

Go back and read the early posts.

Okay the original question:

How to redirect to a route and include a parameter as GET parameter into this route?

Okay answers:

return redirect()->route('postSearch', ['q' => 4]...);

or

return redirect()->route('postSearch', $vurl);

// $vurl is combo of params and or query string

You cannot get any clearer than that.

Cronix's avatar

If @jaheller has more questions about their question, perhaps they will ask for further clarification, or mark the topic as solved.

1 like

Please or to participate in this conversation.