views:

1774

answers:

2

I am trying to create a login system with Zend that when a user tries to access a restricted page they will be taken to a login page if they aren't signed in. The issue I'm having is getting the URL they were trying to access before hand. Is there a Zend Function that will return everything after the base Url? For example, I need "moduleName/controllerName/ActionName/param1/value1/param2/value2" etc etc. to be sent as a querystring param to the login page (ex: login/?redirect=controllerName/actionName/param1/value1/param2/value)

I can get the controller and action name, and I get get the params, but it already includes the module, controller, and action as well. I'd like to just get what I need. I worked out the long way of doing it like this:

$controllerName = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
$actionName = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
$paramArray = Zend_Controller_Front::getInstance()->getRequest()->getParams();
$params = '';

foreach($paramArray as $key => $value)
    $params .= $key . "/" . $value;

$this->_redirect('/admin/login/?redirect=' . $controllerName . "/" . $actionName . "/" . $params);

but even then I end up with params like module/admin/controller/index/ etc which I don't want. So, how can I just get everything as a string like it is in the URL or at least just the params in a string without the controller and action as param values?

*EDIT: Here is my current solution, but there has got to be a more elegant way of doing this *

            $moduleName = $this->getRequest()->getModuleName();
            $controllerName = $this->getRequest()->getControllerName();
            $actionName = $this->getRequest()->getActionName();
            $paramArray = $this->getRequest()->getParams();
            $params = '';

            foreach($paramArray as $key => $value)
                if($key <> "module" && $key <> "controller" && $key <> "action")
                    $params .= $key . "/" . $value . "/";

            $this->_redirect('/admin/login/?redirect=' . $moduleName . "/" . $controllerName . "/" . $actionName . "/" . $params);
A: 

I think you can pass along the last request URI like this:

$this->_redirect('/admin/login/?redirect='.urlencode($this->getRequest()->REQUEST_URI));
Derek Illchuk
This seems like a good candidate but it also returns the baseUrl. Is there any way to just get everything starting with /moduleName/controllerName/actionName/param1/value1/param2/value2 ??
Ryan
To get everything after the baseUrl, I don't have a solution on hand. In my situation, after logging in, I just take the full URI and redirect to it as follows:`$this->_redirect($full_url, array('prependBase'=>false));`Does that help?
Derek Illchuk
Hey Derek, I wasn't aware that _redirect took that array of options with the prependBase option. That's good to know, thanks. For now I have everything setup as mentioned in my edit above, but may switch to this at some point. Thanks.
Ryan
A: 

You could try using urlencode() and urldecode() to encode/decode the individual param strings. An example would be:

// outputs %2Fpath%2Fto%2Ffile.php
$path = '/path/to/file.php';
echo urlencode($path);
cballou
I'm looking for a solution using the Zend Framework. I don't have the path, and I'd like to get all parameters that are included in the original request
Ryan