User-friendly URL for GET (search) form in Laravel 4

Asked

Viewed 2,387 times

2

My question is this:: I have a search form (GET), and would like to know how when running Submit send these parameters to the URL in a friendly way.

Obs.: parameters are not mandatory.

Form:

    {{ Form::open(array('route' => 'neighborhoods.city', 'class' => 'form-inline', 'method' => 'get')) }}

      <div class="form-group col-xs-2 col-lg-2 col-md-2">
        {{ Form::select('state_id', $states, 0, array('class' => 'form-control input-lg', 'id' => 'states')) }}
      </div>

      <div class="form-group col-xs-8 col-lg-8 col-md-8" id="cidades">
        {{ Form::select('city_id', $cities, 0, array('class' => 'form-control input-lg', 'disabled' => true)) }}
      </div>

      <div class="form-group col-xs-2 col-lg-2 col-md-2">
        {{ Form::submit('Buscar', array('class' => 'btn btn-default btn-lg btn-block btn-submit')) }}
      </div>

      {{-- Form::text('search', Input::old('search'), array('class' => 'form-control', 'placeholder' => 'Digite aqui o que você está procurando. Ex.: Encanador'))--}}
    {{ Form::close() }}

3 answers

6


Automatically I do not know if there is any way, I believe that no.

What you can do is, scroll through all the form elements with javascript, mount the url, and have the browser redirect to the url with the filters mounted.

To stay friendly, you can do it in named Routes format: http://laravel.com/docs/routing#named-Routes

Could be something like:

$("form input, form select").each(function(){
    arr[$(this).attr('name')] = $(this).val();
});

Another way is to use Session for the filters, so you don’t need to pass parameters by GET, nor do a POST for each opened page.

  • That’s what I thought too, I don’t really like depending on js, but apparently it’s the best solution.

  • If I didn’t get it wrong, when you’re talking Session is literally putting the data into the session?

  • 1

    Exactly, then you update the data in the session only when you receive a request via POST. But if you need the data to be in a url, so it can be shared already filtered, there is no way, or you do it the way I talked to JS, or leave it in the default format generated by the form

2

I when it comes to a Search, I use the conventional GET, but in case you need a search (Friendly) , a suggestion would be, vc create a page to handle the Search request, redirecting to the search result with the Amicable model .

Example: Submit via Post or GET -> search? name=1&param2=test
redirects to
search/1/test
search/name/test/param2/test

Ai depends on your need.

  • The problem for me there is the following: what is correct? There is a good practice aiming at friendly URL, or do a search like this is "tolerable"?

  • I don’t see any problems in a search like this, so much so that sites like American, submarine and etc generate links that are much more "ugly" than this url pattern generated by GET. There goes taste, and need even (customer asked) :)

  • this redirect scheme is not cool... it just generates more processes and traffic.. A search URL by the GET method , is nothing more than a common URL.. If you want the SEO friendly site, the ideal is to send it already in a friendly format. It is also advisable to send POST mixed with GET..

-3

Do you need these parameters to be passed by URL? Because I have search on my site, and use as follows my method:

downloads/banner

public function postBusca() {
    $word = Input::get('word');

    $articles = Article::where('title', 'LIKE', '%'.$word.'%')
        ->orWhere('body', 'LIKE', '%'.$word.'%')
        ->paginate(6);

    return View::make('articles.busca', compact('articles'));
}

The search data is passed in variable and paged. My URL is www.meusite.com/busca, only.

  • I don’t need the parameters in the URL, so much so that it is currently done using POST. However, in its own way (based on its code) it is insecure. Anyway I understood what you meant. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.