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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi