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

pickab00's avatar

Difference between URL refresh and refresh button

I am currently doing a simple search query with laravel and I was wondering the difference between hitting the reload button and actually refreshing by clicking URL bar and hitting return button. Here is what I've done.

Route::match(['get', 'post'], '/search', 'Main\SearchController@mainSearch');

I am not passing any parameters or such. I am aware that with the current setup, the reload button would do a GET request. This is why I've used match in this situation. I get the current searched results just fine. But if I go on to the URL bar and hit the return button, it gives me all the posts. It is obvious that as there is no set parameter given to search for, and when GET method is used, it just displays all the available data. Now how can I resolve this? Do I 'have' to pass in the search input's data to the route in order for this to not happen?

Whats the best bet here?

0 likes
11 replies
tykus's avatar

If you refresh, you are resending the POST request (typically your browser will ask you to confirm form resubmission), while hitting enter when you have focussed on the address bar is explicitly a GET request.

It might be worthwhile submitting the search form as a GET request in any case - make it bookmarkable/linkable.

pickab00's avatar

@tykus This is what I had in mind:

Route::match(['get', 'post'], '/search/{search}', 'Main\SearchController@mainSearch');

So in my html I am doing something similar to this:

{!! Form::open(['method' => 'POST', 'action' => ['Main\SearchController@mainSearch', 'you shall not pass']]) !!}

<input type="text" name="search" class="form-control front-search" placeholder="Search...">

{!! Form::close() !!}

So my question now is, How would I pass the second parameter for the search. Should I use JS to just take the input value and put it in there? Or is there a right Laravel/PHP way?

tykus's avatar
tykus
Best Answer
Level 104

What is this second parameter intended to be (I am guessing it is the 'you shall not pass' bit)?

Just make form that submits a GET request...

<form method="GET" action="{{ 'Main\SearchController@mainSearch' }}">

    <input type="text" name="search" class="form-control front-search" placeholder="Search...">

</form>

You can check in your controller mainSearch method for the presence of search

if ($request->has('search') {

    // we are searching

    // return view with results

}

// return view without results
1 like
pickab00's avatar

Yes that is the second parameter which is suppose to be the input of search. Here is what I had in mind (If I were to use JS). Doing something like this (Not tested):

var query = $('#form #search').val();
var submit = $('#form');

submit.click(function(e){

e.preventDefault();

    window.location('/search/'+query);

});

And so now my form can be something like this:

<form id="form">
<input type="text" id="search" " class="form-control front-search thaanaKeyboard" placeholder=" Search...">
<input type="submit" id="submit" value="Search">
</form>

Would this do any good? Or should I stick with your suggestion?

tykus's avatar

I think you are making a very simple concept unnecessarily complicated.

pickab00's avatar

That I am always good at doing.

I was actually looking for an alternative other than to display the action, method and other stuff and just completely use js, jquery, ajax etc... My application currently is based off of almost completely ajax and I wanted actually be able to use js for this one. Will using the above method be recommended or should I do this the simple way and save the hassle?

Also, wouldn't using the GET method still return all the data if I visited the /search link without any input?

tykus's avatar

wouldn't using the GET method still return all the data if I visited the /search link without any input?

No, as I mentioned in the last comment, you would check for the presence of a 'search' parameter in the Request. If there is do a search query, otherwise do not. Here is a more complete example:

public function mainSearch(Request $request)
{
    $results = [];

    if ($request->has('search') {

        $results = // do a query that gets the search results using $request->search

    }

    return view('main.search', compact('results'));
}
pickab00's avatar

Here is the updated Route:

Route::get('/search', 'Main\MainController@mainSearch');

Form:

{!! Form::open(['method' => 'GET', 'action' => 'Main\MainController@mainSearch']) !!}
<input type="text" id="search-input" name="search" class="form-control front-search" placeholder=" Search...">
{!! Form::close() !!}

Result (URL):

http://experimental.io/search?search=this+is+my+search

This works great!

Cronix's avatar

clean what up? That's how it's supposed to look if there are spaces

tykus's avatar

This works great!

There's always a BUT...

Is there any way I could cleanup the url?

Not really*.

*You could intercept the form submit using javascript and do some string manipulation on the input value, e.g. replace spaces with underscores, and then undoing that in the mainSearch controller

But why bother... ?search=this+is+my+searchis good enough for google

pickab00's avatar

Yap removed that part. Still on the whole "passing parameter" thing. Meaning /search/what+ever+here kinda thing. But yeah this works the same way and like you mentioned, SEO approved (they really need to come up with emojis) ?

All jokes aside, Thank you!

Please or to participate in this conversation.