0
votes

I would like to retrieve the parameters sent by the form (method="get") and add them to the route.

This is the route:

frontend_list:
path:     /travels/{page}
defaults: { _controller: ProjectFrontendBundle:Frontend:list, page: 1 }

and this is the form:

          <form action="" method="get" class="form_sort" id="myForm">
             <span class="manage_title">Sort by:</span>
                <select class="select_styled white_select" id="sort_list" name="sort" onChange="sendForm();">
                        <option value="">-------</option>
                        <option value="country:asc">Country A-Z</option>
                        <option value="country:desc">Country Z-A</option>
                        <option value="destination:asc">City A-Z</option>
                        <option value="destination:desc">City Z-A</option>
                 </select>
           </form>    

and this is the controller:

    public function listAction($page, Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $nbByPage = $this->container->getParameter('travel.number_by_page');

    if ($request->getMethod() == 'POST')
    {

        $sort = $request->query->get('sort');
        list($orderBy, $orderWay) = explode(":", $sort); //explode 

        $listTravels = $em->getRepository('ProjectTravelBundle:Travel')->getListTravelsFrontend($nbByPage, $page, $orderBy, $orderWay);

        return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
            'listTravels' => $listTravels,
            'page'     => $page,
            'nb_page'  => ceil(count($listTravels) / $nbByPage) ?: 1
        ));
    }

    $orderBy = "id"; // set default orderBy
    $orderWay = "desc"; // set default orderWay

    $listTravels = $em->getRepository('ProjectTravelBundle:Travel')->getListTravelsFrontend($nbByPage, $page, $orderBy, $orderWay);

    return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
        'listTravels' => $listTravels,
        'page'     => $page,
        'nb_page'  => ceil(count($listTravels) / $nbByPage) ?: 1
    ));
}

So I'd like to have url like this for example when select a choice "sort" :

localhost/agence/web/app_dev.php/travels?orderby=country&orderway=aesc

Right now , I get an nonfunctional url like this when select a choice:

localhost/agence/web/app_dev.php/voyages?sort=country%3Aasc

So my question is how to add those parameters in the route frontend_list and add them to the path in twig view beside parameter page to have a correct url with pagination:

                        {% if nb_page > 1 %}
                            {% if  page == 1 %}
                        <a class="link_prev">Previous</a>
                            {% else %}
                        <a href="{{ path('frontend_list', {'page': page - 1}) }}" class="link_prev">Previous</a>
                            {% endif %}

                            {% if  page == nb_page %}
                        <a class="link_next">Next</a>
                            {% else %}
                        <a href="{{ path('frontend_list', {'page': page + 1}) }}" class="link_next">Next</a>
                            {% endif %}
                        {% endif %}
2

2 Answers

2
votes

This is not a good way to create a sortable list, not in symfony.

I suggest you take a look at KnpPaginatorBundle - SEO friendly Symfony2 paginator to sort and paginate.

But, if you need to use code written above. I suggest you do the second select, separate to choose ASC / DESC.

2
votes

I think it is possible, after the installation bundle you must override at the firstly template "KnpPaginatorBundle:Pagination: sortable_link.html.twig". Because the default template is for link, you should create new one for select option. To do this, create in your project structure "app/Resources/KnpPaginatorBundle/views/Pagination/sortable_option.html.twig"

<option {% for attr, value in options %} {{ attr }}="{{ value }}"{% endfor %}>{{ title }}</option>

Now your code will require small modifications:

<form action="" method="get" class="form_sort" id="myForm">
    <span class="manage_title">Sort by:</span>
        <select class="select_styled white_select" id="sort_list" name="sort" onChange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
            {{ knp_pagination_sortable(pagination, 'Country', 'object.countryProperty', {'direction': 'asc'}) }}
            {{ knp_pagination_sortable(pagination, 'Country', 'object.countryProperty', {'direction': 'desc'}) }}
            {{ knp_pagination_sortable(pagination, 'City', 'object.cityProperty', {'direction': 'asc'}) }}
            {{ knp_pagination_sortable(pagination, 'City', 'object.cityProperty', {'direction': 'desc'}) }}
        </select>
</form>

EDIT:

And of course You have to change the path in the configuration file

knp_paginator:
    page_range: 5                      # default page range used in pagination control
    default_options:
        page_name: page                # page query parameter name
        sort_field_name: sort          # sort field query parameter name
        sort_direction_name: direction # sort direction query parameter name
        distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
    template:
        pagination: KnpPaginatorBundle:Pagination:sliding.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_option.html.twig # sort option template
        #sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template (default)