Reputation: 18109
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);
Upvotes: 5
Views: 18352
Reputation: 2011
No idea why Zend doesn't have this feature, but this is how I have accomplished it. My use case was that i wanted to redirect old URLs that went directly to controllers using the default routing, to a new API url structure that used routes.
$filtered_params = array_diff_key(
$this->_request->getParams(),
array_fill_keys( array('controller', 'module', 'action' ), 1)
);
$this->_redirect('/api/1.0/?' . http_build_query($filtered_params));
Upvotes: 2
Reputation: 364
Old question but heres a solution thats worked for me. From here you may want to url encode it etc
$currentRoute = substr($this->getRequest()->getRequestUri(),strlen($this->getRequest()->getBaseUrl()));
Upvotes: 1
Reputation: 147
i know its an old question but for future references:
$this->getRequest()->getRequestUri();
that should get you what you want..
Upvotes: 2
Reputation: 43457
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);
Upvotes: 0
Reputation: 5658
I think you can pass along the last request URI like this:
$this->_redirect('/admin/login/?redirect='.urlencode($this->getRequest()->REQUEST_URI));
Upvotes: 3