Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect to last available page, if a non existent page-number was requested? #477

Closed
spackmat opened this issue Mar 13, 2018 · 5 comments
Closed

Comments

@spackmat
Copy link

Hi,

in my application, I implemented a graceful handling of the situation, when a non existant (too high) page number was requested (e.g. when one changes the perPage-value on the last page). In that case, I return a redirect to the last available page. This looks like this:

    // redirect to last available page, when a non existent page was requested
    if ($pagination->count() === 0 && $pagination->getTotalItemCount() > 0) {
        return $this->redirect($this->generateUrl($request->attributes->get('_route'), arrray_merge($request->attributes->get('_route_params'), ['page' => ceil($pagination->getTotalItemCount() / $request->query->getInt('perpage', 10))]))); // change the param
    }

This boilerplate is used in all my controllers and I want to abstract this check. For now, I would call a static utility function (or a service-method) in my controllers, which throws a (custom) RedirectException in that case. Like this one:

class PaginatorUtils {
    // redirect to last available page, when a non existent page was requested
    public static function redirectToLastAvailablePage(Knp\Component\Pager\Pagination\PaginationInterface $paginator, Symfony\Component\HttpFoundation\Request $request, ) {
        if ($pagination->count() === 0 && $pagination->getTotalItemCount() > 0) {
            throw new App\Exception\RedirectException(
                new Symfony\Component\HttpFoundation\RedirectResponse($router->generate(
                    $request->attributes->get('_route'),
                    array_merge(
                        $request->query->all(), 
                        $request->attributes->get('_route_params'),
                        ['page' => ceil($pagination->getTotalItemCount() / $request->query->getInt('perpage', 10))]
                    )
                ))
            );
        }
    }
}

For details of that custom redirectException, see https://www.trisoft.ro/blog/56-symfony-redirecting-from-outside-the-controller

My question is now: Wouldn't that be a nice feature of the Paginator itself? Maybe configureable? To perform a redirect (preserving all other request-parameters, but the page number) on its own, when a non available page is called?

@nicolasmure
Copy link
Contributor

Hello :)
Sorry for the late answer, do you mind to make a pull request ?
This is indeed a nice feature :)

@lfaveur
Copy link

lfaveur commented Sep 4, 2018

Hello will be a nice improvment ;)

@spackmat
Copy link
Author

spackmat commented Sep 4, 2018

Hi, I am not sure where and how this should be implemented within the existing code. And how to make this configurable to make it only available, when it is correctly handled. Existing applications must listen to such a RedirectException and handle it correctly, so this cannot be enabled by default, although the current default behavior of the case also isn't very graceful (throwing a 404). So it should't be an enable-and-forget-feature. Or, a I suggested, the paginator could do the redirect on its own. Doesn't sound like a clean solution.

Any thoughts on this? Should the Paginator respond with a custom RedirectException that must be handled by the application? Where and how could that be implemented. I am not that familiar with the Paginator's codebase.

@garak
Copy link
Collaborator

garak commented Jul 21, 2019

@spackmat it could be implemented with a custom Exception inside the library, something like PaginationOutOfBundleException.
Then a listener to such exception could be added to the bundle (ot it can be simply documented and left to the user)

@garak
Copy link
Collaborator

garak commented Jul 8, 2020

Fixed in related PR

@garak garak closed this as completed Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants