You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
I want create redirect to last available page when trying open non-existent page (OutOfRangeCurrentPageException). Currently I've code like this
<?phpnamespaceMetal\ProjectBundle\Pagerfanta\EventListener;
usePagerfanta\Exception\NotValidCurrentPageException;
usePagerfanta\Exception\OutOfRangeCurrentPageException;
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
useSymfony\Component\HttpFoundation\RedirectResponse;
useSymfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
useSymfony\Component\HttpKernel\Exception\NotFoundHttpException;
useSymfony\Component\HttpKernel\KernelEvents;
useSymfony\Component\Routing\Generator\UrlGeneratorInterface;
classHandleOutOfRangeCurrentPageimplementsEventSubscriberInterface
{
private$router;
publicfunction__construct(UrlGeneratorInterface$router)
{
$this->router = $router;
}
/** * @param GetResponseForExceptionEvent $event */publicfunctiononException(GetResponseForExceptionEvent$event)
{
$rootException = $exception = $event->getException();
do {
if ($exception instanceof OutOfRangeCurrentPageException) {
preg_match('/"(\d+)"$/', $exception->getMessage(), $matches); // this hack can be easy removed: create field "maxAvailablePage" in exception class$maxPage = $matches[1];
$request = $event->getRequest();
$url = $this->router->generate(
$request->attributes->get('_route'),
array_merge(
$request->attributes->get('_route_params'),
$request->query->all(),
array('page' => $maxPage) // we have hardcoded page parameter name here
),
true
);
$event->setResponse(RedirectResponse::create($url, 301));
return;
}
if ($exception instanceof NotValidCurrentPageException) {
$event->setException(newNotFoundHttpException('Page Not Found', $rootException));
return;
}
} while ($exception = $exception->getPrevious()); // Twig wraps actual exception into his own
}
/** * {@inheritDoc} */publicstaticfunctiongetSubscribedEvents()
{
returnarray(
KernelEvents::EXCEPTION => array('onException', 512)
);
}
}
As you can see from code above, main problem is that we cann't dehardcode parameter for page name. Has anybody any suggestions for how to deal with it?
I think, that we can create method like Form::handleRequest, something like $pagerfanta->handleRequest($request, 'nameOfPageParameter') and then use this parameter on route generation.
The text was updated successfully, but these errors were encountered:
This is follow up of #60.
I want create redirect to last available page when trying open non-existent page (
OutOfRangeCurrentPageException
). Currently I've code like thisAs you can see from code above, main problem is that we cann't dehardcode parameter for page name. Has anybody any suggestions for how to deal with it?
I think, that we can create method like
Form::handleRequest
, something like$pagerfanta->handleRequest($request, 'nameOfPageParameter')
and then use this parameter on route generation.The text was updated successfully, but these errors were encountered: