3
votes

In Zend Framework I have an Action Helper that loads a login form on most pages. This happens in the preDispatch() method of the Helper and I want to setAction() on the form so that it posts back to the current URL.

What's the best way to access the current URL / route from within the Action Helper? Access the Request (via the Action Controller), then pull then getActionName() and getControllerName(), and concatenate them with baseURL()?

Is there a simpler way? (Set action requires the URI string as a parameter).

Thanks!

3

3 Answers

5
votes

You can do as @Elie suggested. However, if you want to use ZF methods for this, you can have a look at this:

    $request = Zend_Controller_Front::getInstance()->getRequest();
    echo $request->getHeader('referer'); // referer's address
    echo $request->getRequestUri();      // current address
2
votes

I found that I didn't need to access the current URL / route from within the Action Helper. By leaving the form action blank, it automatically posts to the current URL. Perfect.

0
votes

If I understand correctly, when the user logs in, you want to send them back to the page where they came from. The code I use to do this is:

// the user has come from a particular page - send them back
if($_SERVER['HTTP_REFERER']) {
    $this->_redirect($_SERVER['HTTP_REFERER']);
} else {
// the user has come from the home page, or this page
    $this->_redirect('/');
}

which is located in the login action (i.e. LoginController->loginAction()).