Use _GET instead of _POST for searches? (Edit boilerplate?)

I love using GET since users can bookmark/email links to search results. If I use POST, my clients send me a link to their search results and don't understand why I don't see what they see. ;)

Is there any way to make it use _GET as the default? As it is I'm having to edit the searchAction every time to have it read the vars from _GET, and I'm also having to edit the pagination in the view. For that I wrote a static getVarsFiltered method in my Utils class that returns a filtered, query-string friendly version of the get vars:

$vars = Utils::getVarsFiltered('page');

OR, is there a way to setup my own copy of the boilerplate for it to use? Obviously I could fork the repo, edit and use that, but then I have to maintain my fork to keep it up-to-date...



4.6k
edited Feb '16

I came acoss the same problem, and found some site,like kickasstorrent,using jquery to transform the post request to a get url request.

Then you can get the search keywords from route parameters in phalcon.

It would have to also modify the pagination links. Sounds like a nasty hack, I'd rather deliver the right HTML to begin with.



4.6k
edited Feb '16

Just for your referece, below is how I do the trick.

1, you should have a page contains search form, somethiing like:

             <ul class="nav navbar-nav navbar-right">
                <li>
                    <form id="search-form" class="navbar-form navbar-left" role="search">
                        <div class="form-group">
                            <?php $search = isset($search) ?$search: null?>
                            {{ text_field("search",'class':'form-control','placeholder':'Search','value':search) }}
                        </div>
                        <button type="submit" class="btn btn-default">查询</button>
                    </form>
                </li>
            </ul>

2, using some js to get the post request to get request.

  $(function() {
    $("#search-form").submit(function() {
      var keywords;
      keywords = $("#search").val().trim();
      keywords = keywords.replace(/\//, ' ');
      location.href = "http://" + location.host + ("/search/" + keywords);
      return false;
    });

3,In order to get this url working, you should define some route like :

$router->add('/search/{search:[^/]+}','standards::search')->setName('standards.search');
$router->add('/search/{search:[^/]+}/page/{page:[0-9]+}','standards::search')->setName('standards.search.page');

4,And the controller should be something like :

public function searchAction($search,$page = 1)
    {

        if(myToolsFacade::isStandardNumber($search)) {
            $file = Files::findByStandardNumber($search);
            if($file) return $this->redirectByRoute(['for'=>'standards.show','file'=>$file->id]);
        }

        $this->view->page = $this->getPaginatorByQueryBuilder(Files::searchQuery($search),50,$page);
        $this->view->search = $search;
    }

5,In the (search.volt) view file, you could use the $page variable to set the navigate link, like :

{% block nav %}
    <div><span class="label label-primary">共计{{ page.total_items }}条标准</span>--<span class="label label-primary">第{{ page.current }}页/总{{ page.total_pages }}页</span></div>
    {% if page.total_items > page.limit %}
        <nav>
            <ul class="pager">
                <li class="previous"><a href="{{ url(['for':'standards.search.page','search':search,'page':page.before]) }}"><span aria-hidden="true">&larr;</span> 上一页</a></li>
                <li class="next"><a href="{{ url(['for':'standards.search.page','search':search,'page':page.next]) }}">下一页 <span aria-hidden="true">&rarr;</span></a></li>
            </ul>
        </nav>
    {% endif %}
{% endblock %}


4.6k

The code above is how I do the trick. I'm wondering how you do your trick?

It would have to also modify the pagination links. Sounds like a nasty hack, I'd rather deliver the right HTML to begin with.