1

Say I want to be able to submit a search form on any page that will append a ?s= to the current url but return a SERP: https://example.com/my-page?s=foobar. (I've seen a few sites do this instead of pointing to /search?s=.* - not the way I'd do it, but it illustrates my question.)

In my Laravel web.php routes, is there currently a way to register this other than maybe using a wonky regex?

<?php
Route::get('.+?\?.+?\bs={search}', 'SearchController@search');
// This regex is probably wrong, but you get what I was going for,
// and that kinda highlights why this is not an ideal way to do it
?>

This gets uglier when, say, you want to capture multiple $_GET params:

https://example.com/my-page?s=foobar&dept=clothing

I haven't found anything in Laravel docs that let you define query string options on the Route $uri parameter. The recommended option is to just use pretty URLs (https://example.com/search/foobar), but there are definitely valid use cases for keeping query strings.

2
  • 1
    So you want any route that has a ?s=... to be picked up by a single search controller? Is that what you're trying to do? I don't really undersatnd why you would structure an app this way, but you're probably better off adding middleware that looks for the s query param Commented Dec 11, 2018 at 19:48
  • @WillardSolutions yep, that looks to be the answer. Yeah, my example is not great. Perhaps I should fix it up with a valid use-case. Like /?keyword=foobar&dept=clothing&brand=acme, where if brand=acme it would use AcmeController or something. Commented Dec 11, 2018 at 20:40

2 Answers 2

1

1: Create a middleware (https://laravel.com/docs/5.7/middleware) 2: Validate incoming request using $request->query('s') 3: If validation successfull return to next else redirect to wherever or display an error response

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh I believe this is exactly the answer. I've seen others ask this question (usually about the route() helper) all around the interweb and most answers are "Just don't do it that way" instead of recognizing the valid use-cases of query strings. I've not yet dug into Middleware, but that looks to be the appropriate route.
0

That is very simple that depends on the parts of a url. The Route class uses the "path string" of the url and you try to use the "parameters" of the url.

https://doepud.co.uk/blog/anatomy-of-a-url

In modern websites you should work with that structure because you get a better URL structure and it's much better for SEO and search engines.

You should use the parameters only in the function you call for small things which you can call over the $request->get('s'); method but then you have to redirect or you have to work in that function.

So don't fight the framework and work in that structure what is defined from the framework that all people who know the framework know how to work with it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.