Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jpeterson579's avatar

How to allow user to do a search which returns a route with the search query in it?

Hi All!

I have a search box that allows users to input a book name and then gives them a list of books that match that query.

What I want to be able to do is after the search form is submitted it will direct them to a route:

search/book/{searchQuery}

How could I do this?

Furthermore, what if the user searches something that is multiple words? Or special characters? Won't this break the URL since there will be spaces or foreign characters in it now? How can I combat this?

0 likes
5 replies
jpeterson579's avatar

@Snapey If a user wants to share a link to his results. The person he shared it with would not have to do the search part again but could follow the link.

Also for SEO and search engines. If these pages were linked to, search engines could easily crawl the results for specific queries since there is an actual url associated with it...

Does that make sense or am I thinking about this all wrong?

Snapey's avatar

no that's fine. I was wondering if encoding the query for use in the url would break the use of it.

if your search form uses GET instead of post then the url will contain the query

jpeterson579's avatar

@Snapey

So using GET will give me a url like this

http://localhost:8888/search?_token=YbhH1RM56npIRBk5TSpf41fLOj83a6VDQlpdRLNS&author=JK+Rolling&all=on

How do I get rid of the token in this url? I want the form to require a token but dont want to to display on the url

Snapey's avatar

you don't need the token if the form does not change state, and actually it is not even checked by Laravel on GET requests

1 like

Please or to participate in this conversation.