简体   繁体   中英

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 members.phtml page.
Now I want to add a back button on members.phtml 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 members.phtml page?

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:

http://www.example.com/members.phtml?previous=my_groups

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';
$this->redirect()->toRoute(
    'members', 
    array(
        '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.

UPDATE

Some extra information after your comment...

You can find current page url for example by using:

$this->getRequest()->getRequestUri();

Or you can get the current matched route:

$this->getMvcEvent()->getRouteMatch()->getMatchedRouteName();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM