PaginateViewHelper redirect to request page

226 Views Asked by At

I'm using TYPO3\Fluid\ViewHelpers\PaginateViewHelper in my index action which listing some items. Each item has actions which are done on the fly, so after action is called and processed it's back to the index action.

public function deleteAction(Item $item) {
    $this->itemService->remove($item);
    $this->redirect('index');
}

Unfortunately in this case I'm getting redirected to the first page of index. Is there possibility to redirect to the page where I'm triggering delete action? Can I also obtain arguments of subrequest send to PaginateController?

I know I can use AJAX call instead or write my own pagination, but I'd like to use existing solution if it's possible.

2

There are 2 best solutions below

0
On

You can do something like this

$referringRequest = $this->request->getReferringRequest();
if ($referringRequest !== null) {
    $this->redirectToRequest($referringRequest);
}

This only works, if the referring request is sent with the original request, which is the case for Fluid Forms, which you should use for unsafe requests (POST, PUT, DELETE).

1
On

To keep the GET Parameters you can use addQueryString="1" in the LinkViewHelper... maybe this is already enough to keep the current page when using redirect() in the controller.

Otherwise you can use the UriBuilder of the controller to generate a URI including the desired page in your pagination and other parameters and then use the function redirectToUri() to redirect the user to the desired URI.

https://typo3.org/api/typo3cms/class_t_y_p_o3_1_1_c_m_s_1_1_extbase_1_1_mvc_1_1_web_1_1_routing_1_1_uri_builder.html