doubingguan3425 2014-04-16 16:00
浏览 42
已采纳

Symfony 2搜索表单路由设置

I am making a basic search form with symfony 2. I have problem with configuration of routing.yml file.

My routing.yml File

adhl_front_search:
    pattern: /{_locale}/search
    defaults: { _controller: AdhlFrontBundle:Blog:search  }
    requirements:
        _locale: en

My form code:

<form method="get" action="{{ path('adhl_front_search') }}/">
      <input type="text" value="" name="keyword" />
      <input type="submit" value="Search" />
</form>

I get and want this url:

app_dev.php/en/search/?keyword=computer

Symfony Error:

No route found for "GET /en/search/" 

I don't know how to configure my url for search form. In my case it expect ?keyword=computer in routing.yml. If i do it like pattern: /{_locale}/search/{keyword} then form page gives error at {{ path('adhl_front_search') }}

Secondly, how can i pass the keyword (computer in above case) value to my controller? Please help me to sort out this problem.

  • 写回答

1条回答 默认 最新

  • drpfu51608120170 2014-04-16 16:05
    关注

    the most simple way to do this is

    pattern: /{_locale}/search/?keyword={key}
    

    and in your controller expect the $key variable, but this isn't the best way ...

    however try to do this

    pattern: /{_locale}/search/
    

    I have this in one of my projects and it works

    edit 2: try this in your controller

      public function searchAction(Request $request)
        {   
            $keyword = $request->get('keyword');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?