Symfony 2 search form routing setting

2019-08-29 08:28发布

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条回答
Explosion°爆炸
2楼-- · 2019-08-29 09:00

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');
查看更多
登录 后发表回答