1

Not sure if I properly wrote the subject but anyway.

Since you can create specific routes with different parameters for eg:

_search:
    pattern: /page/{category}/{keyword}
    defaults: { _controller: Bundle:Default:page, category: 9, keyword: null }

is there any way from a form with GET method to get to that route specific url format?

At the moment the url is like /page?category=2?keyword=some+keyword

As such is not passing to the route format as you may noticed.

What do I need to do to get it working through this specific format? I really have no idea how to rewrite the page url to match the route settings for the specific url. Even in plain php was stumbled on this ...

Thanks in advance.

3 Answers3

5

It's the default behavior of HTML forms with GET method. You will need to build that URL yourself.

Backend way

  • Drawback: It makes two requests to the server instead of one
  • Advantage: It's more maintainable because the URL is built using the routing service

Your routing file

_search:
    pattern: /page/{category}/{keyword}
    defaults: { _controller: Bundle:Default:page, category: 9, keyword: null }

_search_endpoint:
    pattern: /page
    defaults: { _controller: Bundle:Default:redirect }

Your controller

public function redirectAction()
{
    $category = $this->get('request')->query->get('category');
    $keyword = $this->get('request')->query->get('keyword');

    // You probably want to add some extra check here and there
    // do avoid any kind of side effects or bugs.

    $url = $this->generateUrl('_search', array(
        'category' => $category,
        'keyword'  => $keyword,
    ));

    return $this->redirect($url);
}

Frontend way

Using Javascript, you can build the URL yourself and redirect the user afterwards.

  • Drawback: You don't have access to the routing service (although you could use the FOSJsRoutingBundle bundle)
  • Advantage: You save one request

Note: You will need to get your own query string getter you can find a Stackoverflow thread here, below I'll use getQueryString on the jQuery object.

(function (window, $) {
    $('#theFormId').submit(function (event) {
        var category, keyword;

        event.preventDefault();

        // You will want to put some tests here to make
        // sure the code behaves the way you are expecting

        category = $.getQueryString('category');
        keyword = $.getQueryString('keyword');

        window.location.href = '/page/' + category + '/' + keyword;
    }):
})(window, jQuery);
Community
  • 1
  • 1
Thomas Potaire
  • 6,166
  • 36
  • 39
  • Yes, thank you. Before I've saw the solutions I already went and made the frontend way using JS. Seems like there's really no 'standard' way other than making the url yourself or redirecting it. Thank you again, good answer too! – Vasile-Bogdan Raica Mar 16 '14 at 14:15
0

You could add a second route that will just match /page

then in the controller you can get the defaults. and merge them with any that are passed.

Take a look at a similar question I answered for some code examples.

KendoUI Grid parameters sending to a symfony2 app

Community
  • 1
  • 1
Chase
  • 9,289
  • 5
  • 51
  • 77
  • I don't believe that would help, sorry. In this case, what I wanted is when you click submit in the form, to have the url: /page?category=2?keyword=some+keyword changed to /page/{category}/{keyword} page is the action file from the form, just so you know. May be wrong, I think this has something to do with rewrites rather then symfony :-/ – Vasile-Bogdan Raica Mar 15 '14 at 01:26
0

I've too encountered this issue and I managed to resolve it with a slightly different solution.

You could also reroute like @Thomas Potaire suggested, but in the same controller, beginning your controller with :

/**
 * @Route("/myroute/{myVar}", name="my_route")
 */
public function myAction(Request $request, $myVar = null)
{
    if ($request->query->get('myVar') !== null) {
        return $this->redirectToRoute('my_route', array(
            'myVar' => str_replace(' ','+',$request->query->get('myVar')) // I needed this modification here
        ));
    }
    // your code...
}
ThEBiShOp
  • 506
  • 8
  • 23