Symfony 2搜索表单路由设置 [英] Symfony 2 search form routing setting
问题描述
我正在用symfony 2制作基本的搜索表单.我对routing.yml文件的配置有疑问.
I am making a basic search form with symfony 2. I have problem with configuration of routing.yml file.
我的routing.yml文件
adhl_front_search:
pattern: /{_locale}/search
defaults: { _controller: AdhlFrontBundle:Blog:search }
requirements:
_locale: en
我的表单代码:
<form method="get" action="{{ path('adhl_front_search') }}/">
<input type="text" value="" name="keyword" />
<input type="submit" value="Search" />
</form>
我得到并想要这个网址:
app_dev.php/en/search/?keyword=computer
Symfony错误:
No route found for "GET /en/search/"
我不知道如何配置搜索表单的网址.在我的情况下,它会在routing.yml中期望?keyword = computer.如果我像 pattern:/{_ locale}/search/{keyword}
那样操作,则表单页面在 {{path('adhl_front_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.
推荐答案
最简单的方法是
pattern: /{_locale}/search/?keyword={key}
,并且在您的控制器中期望$ key变量,但这不是最好的方法...
and in your controller expect the $key variable, but this isn't the best way ...
但是尝试这样做
pattern: /{_locale}/search/
我在我的一个项目中拥有它,并且可以正常工作
I have this in one of my projects and it works
修改2:在您的控制器中尝试一下
edit 2: try this in your controller
public function searchAction(Request $request)
{
$keyword = $request->get('keyword');
这篇关于Symfony 2搜索表单路由设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!