Symfony2: Why weren't query string parameters included in the routing component?

I am porting an outdated application to Symfony2, and I am struggling because routing does not include query string parameters. Some quick examples. Suppose you have a book search page in which you can filter the results by criteria:

http://www.bookstore.com/books?author=Stephen+King&maxPrice=20

The good thing about query string parameters in this case is that you can have any number of filters, use the ones you want for any given query, and not interpret URLs with filters that you don't use.

Let's say we rewrote the route for the above request using the Symfony2 routing component. It might look like this:

http://www.mybookstore.com/book/any_title/stephen%20king/any_release_date/max_price_20/any_min_price/any_format/any_category

Even without taking into account how arbitrarily long the unclean URL is, I still donโ€™t find it intuitive, because each โ€œsegmentโ€ of this route is not a pair of key values, but instead just a value (for example, author=Stephen+King > /stephen%20king/ ).

You can, of course, access the request lines in the controller by passing the Request object to the action method (for example, indexAction(Request $request) { ), but then checking them and passing them to other parts of the application becomes difficult (i.e. where I find myself now). What if you use the Knp Menu Bundle package to create a sidebar and want the parts to be marked as .current based on the query string parameters? There is no functionality for this, just functionality for integrating with Symfony2 routes.

And how to check the correctness of the query string parameters? I am currently considering them as a form in order to submit them to a database to generate a query. Maybe this is how the Symfony2 team intended to handle them? If so, I just really would like to know why. Looks like I'm struggling with the app.

+4
source share
2 answers

I eventually asked Fabien to answer this question in Symfony Live San Francisco 2012. At the same conference, there was another conversation on this issue, and I will share the slides with you here:

http://www.slideshare.net/Wombert/phpjp-urls-rest#btnNext

Basically in the slides you can see that the author agrees with me that the query string parameters should be used for filtering. They should not be used to determine the source of content. Thus, the route should point to the product (or something else), and the query string parameters should then be used in your controller to apply any logic necessary to filter this content source (according to Fabien).

I ended up creating an entity in my application to bind all the parameters of the query string and then manipulate it, similar to how forms are processed. In fact, when you think about it, it's basically the same thing.

+2
source

As with Symfony1, query strings are independent of route parameters.

If you have a path defined as @Route("/page/{id}", name="single_page") , you can create a path in your view as follows:

{{ path('single_page', { id: 3, foo: "bar" }) }}

The resulting URL will be /page/3?foo=bar .

0
source

Source: https://habr.com/ru/post/1435357/


All Articles