Y
Y
yalex14422015-11-17 12:42:03
symfony
yalex1442, 2015-11-17 12:42:03

How to use get parameters in route anotations sf2?

Hello.
There is such a route;

/**
     * @Route("/search.{_format}/media/{tagName}",
     *  name="searchMedia",
     *  requirements= {
     *  "size":"thumbnail|low_resolution|standard_resolution",
     *  "tagName":"\w+",
     *  "count":"\d+",
     *  "tagId":"\d+",
     *  "_format":"web|json"
     * },
     *  defaults={
     * "count" : "33",
     * "tagId" : "0",
     * "size":"thumbnail",
     * "_format":"web"
     * }
     * )
     */

I want to generate links containing get parameters, but when generating links in the twig template, the values ​​​​described in requirements are not set ,
but get parameters are inserted without problems that are not described in requirements and {slug} parameters in the route.
for example from:
{{ path('searchMedia', ({tagName: tag.name ,param1:"value1",tagId:"100500"})) }}
we get:
/search.web/media/ {tag.name} ? param1=value1
In Google, it is advised to generate links in the controller, and therefore check the input parameters in the controller.
I would like to leave all input parameter checks in route anotations
Is there a way to write a route something like this:
/**
     * @Route("/search.{_format}/media/{tagName}",
     *  name="searchMedia",
     *  requirements= {
     *  " GET(size)":"thumbnail|low_resolution|standard_resolution",
     *  "tagName":"\w+",
     *  "GET(count)":"\d+",
     *  "GET(tagId)":"\d+",
     *  "_format":"web|json"
     * },
     *  defaults={
     * "GET(count)" : "33",
     * "GET(tagId)" : "0",
     * "GET(size)":"thumbnail",
     * "_format":"web"
     * }
     * )
     */

and so that the link in twig is generated based on this route?
******************************************************* ************************************
I decided to do everything traditionally using sf fom validate
route I will shorten it to
/**
* @ Route("/search.{_format}/media/{tagName}",
* name="searchMedia",
* requirements= {
* "_format":"web|json"
* },
* defaults={
* "_format": "web"
* }
* )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-11-17
@yalex1442


{{ path(name, parameters, relative) }}
name
type: string
parameters
type: array default: []
relative
type: boolean default: false
{{ path('searchMedia', ({tagName: tag.name,param1:"value1",tagId:"100500"})) }}
{{ path('searchMedia', (...)) }} brackets?
{{ path('searchMedia', {tagName: tag.name, param1:"value1", tagId:"100500"} ) }}
size, count, tagId in requirements are ignored as they are not in /search.{_format} /media/{tagName}

E
Evgeny Svirsky, 2015-11-17
@e_svirsky

It seems to me or you have extra brackets here:
It should be like this:

{{ path('searchMedia', {tagName: tag.name,param1:"value1",tagId:"100500"}) }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question