Posted By

jiewmeng on 07/27/10



Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Zend Framework: Having forms redirect back to page user came from

 / Published in: PHP

redirecting back to where the user came from is a very common functionality in login forms.

technical implementation:

  • have a hidden field (returnUrl)
  • populated by current url (in Zend Framework, i get this using the request's getRequestUri() function)
  • in the AuthController, if the returnUrl field is set, i will redirect the user to that page using the Redirector helper.
  1. // Login form class
  2. $controllerFront = Zend_Controller_Front::getInstance();
  3. $returnUrl = $controllerFront->getRequest()->getRequestUri();
  4. $this->addElement('hidden', 'returnUrl', array(
  5. 'value' => $returnUrl
  6. ));
  8. // AuthController
  9. // inside loginAction() - after authentication is successful
  10. $returnUrl = $form->getElement('returnUrl')->getValue();
  11. if (!empty($returnUrl)) {
  12. $this->_helper->getHelper('Redirector')->setGotoUrl($returnUrl);
  13. }

Report this snippet  

You need to login to post a comment.