Passing route url format to symfony2 form with get method

后端 未结 3 517
一生所求
一生所求 2021-01-23 10:36

Not sure if I properly wrote the subject but anyway.

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

_search:
    pattern: /page/{         


        
相关标签:
3条回答
  • 2021-01-23 10:52

    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

    0 讨论(0)
  • 2021-01-23 11:03

    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...
    }
    
    0 讨论(0)
  • 2021-01-23 11:05

    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);
    
    0 讨论(0)
提交回复
热议问题