Chaitanya Sairam Chaitanya Sairam - 1 year ago 251
PHP Question

How to redirect user to previous page zend framework 3

I am designing a website, that has my groups and other groups that are present on two different webpages. Both my groups and other groups have an option to see the members of each group which is redirecting to a


Now I want to add a back button on
page at the top that will go to the previous page (my groups/other groups). Is there a way to save the current url and send it to

Answer Source

You could use the redirect plugin and add a query param previous to the url. Check this answer for an example and check the ZF2 documentation here on the redirect plugin. It would become something like this:

In the controller for your my_groups route where you added the redirect url to your members page you will need to add the query param to the redirect route:

$previous = 'my_groups';
        'action' => 'index'
    array( 'query' => array(
        'previous' => $previous

Now when the members.phtml page is requested you can find the previous page inside the query parameters of the request object. You can easily access it in your controller like this:

$previous = $this->params()->fromQuery('previous'); // my_groups

Now you know whether the members page was accessed from my_groups or other_groups and you can render the back button accordingly.


Some extra information after your comment...

You can find current page url for example by using:


Or you can get the current matched route:


And you can use this to dynamically add the previous url.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download