【问题标题】:Laravel :How to search for item from the main page search barLaravel:如何从主页搜索栏搜索项目
【发布时间】:2014-03-02 21:57:45
【问题描述】:

我的母版页上有一个搜索栏。所有其他页面都在扩展此页面。 我在母版页上有一个搜索文本字段,它是这样定义的

{{Form::open(array('action'=>'TopicsController@searchquery', 'method'=>'GET'))}}      
    {{Form::input('search', 'q', null, ['placeholder'=>'Search', 'class' => 'search'])}}
{{Form::close()}}

Routes.php 入口如下:

Route::get('/searchquery/{q}', [
    'uses'=> 'TopicsController@searchquery',
    'as' => 'topic.searchquery']);

控制器动作看起来像这样

    public function searchquery($q)
{

    if($search = Request::get('q')){

        $topics = Topic::search($search);
    }
    else {
        $topics = Topic::all();
    }

    $categories = Category::all();
    $tags = $this->topicRepo->allTags();
    return View::make('topics.topicSearch')->withCategories($categories)->withTopics($topics)->with('term', $q)->withTags($tags);

}

但是,当我执行搜索时,控制器的搜索查询中的搜索查询没有得到正确的参数。
我的网址如下所示:http://localhost:8000/searchquery/%7Bq%7D?q=test

请帮忙。

谢谢

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    解决这个问题的一种方法是从路由定义中删除/{q},现在它应该可以正常工作了。但我猜你希望它有漂亮的方式,/q/term,而不是像?q=term。好吧,普通的 HTML 表单提交是无法做到的,但是使用一点 JS 就可以做到:

    // jQuery, but it can be done without it
    $('form').submit(function() {
      document.location.href = '/searchquery/' + $(this).find('input').val();
      return false;
     });
    

    如果您选择了最后一种解决方案,您可以考虑不在 JS 代码中硬编码路由,而是可以在表单上的数据属性(例如data-submit-url)中设置路由。

    【讨论】:

    • 我想知道使用查询参数是否是我前进的方向。这就是我打算做的..
    猜你喜欢
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多