How to prevent KNP Pagination Parameters persist between requests once they are defined?

39 Views Asked by At

In my Symfony application i run into the problem that paginator variables are passed around the entire application which results in duplicate content and URLs with parameters that should not be there.

Example: I open the route to my blog-articles like : url.tld/blog Blogitems are paginated with KNP Paginator bundle so url.tld/blog?blogpostpage=2 also works as intended. When i open a specific Blog Post like url.tld/blog/post/postname or switch to a different route like url.tld/products the "?blogpostpage=2" parameter persists in the URL which is not intended.

Why is that and how can i prevent that?

BlogController:

#[Route('{_locale}/blog', name: 'blog-overview')]
    public function blogoverviewAction(Request $request, \Knp\Component\Pager\PaginatorInterface $paginator)
    {
        $posts = new DataObject\Blog\Listing();
        $posts->setOrderKey("o_creationDate");
        $posts->setOrder("desc");

    $paginator = $paginator->paginate(
        $posts,
        $request->get('blogpostpage', 1),
        3
    );

        return $this->render(
            'blog/blogoverview.html.twig',
            array(
                "posts" => $paginator,
                'paginationVariables' => $paginator->getPaginationData()
            )
        );
    }

Blogoverview Twig Template:

{% extends "layout.html.twig" %}
....
// link to blogpost-Detail
 <h3 class="fs-18 text-heading lh-194 mb-1">
                        <a href="{{ pimcore_url({ 'post_id': post.id, 'post_name': post.headline}, 'blog-detail') }}" class="text-heading hover-primary">
                          {{ post.headline }}
                        </a>
                      </h3>

// Pagination on page
   <nav class="pt-4" aria-label="Pagination">
              <ul class="pagination rounded-active justify-content-center mb-0">
                {% if(paginationVariables.previous is defined) %}
                  <li class="page-item">
                    <a class="page-link prev" href="{{ pimcore_url({'page': paginationVariables.previous}) }}" aria-label="Previous">
                      <i class="far fa-angle-double-left"></i>
                    </a>
                  </li>
                {% endif %}
                {% for page in paginationVariables.pagesInRange %}
                  {% if(paginationVariables.current == page) %}
                    <li class="page-item active" aria-current="page">
                      <span class="page-link">
                        {{ page }}
                        <span class="sr-only">
                          (current)


                        </span>
                      </span>
                    </li>
                  {% else %}
                    <li class="page-item">
                      <a class="page-link" href="{{ pimcore_url({'blogpostpage': page}) }}">
                        {{ page }}
                      </a>
                    </li>
                  {% endif %}
                {% endfor %}
                {% if(paginationVariables.next is defined) %}
                  <li class="page-item">
                    <a class="page-link next" href="{{ pimcore_url({'blogpostpage': paginationVariables.next}) }}" aria-label="Next">
                      <i class="far fa-angle-double-right"></i>
                    </a>
                  </li>
                {% endif %}
              </ul>
            </nav>

At the moment i have no idea where to look for a solution. Is it a condiguration question or something in the controller?

0

There are 0 best solutions below