简体   繁体   中英

Zend - Get everything after base Url (Controller, Action, and any params)

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);

我想你可以像这样传递最后一个请求URI:

$this->_redirect('/admin/login/?redirect='.urlencode($this->getRequest()->REQUEST_URI));

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));

i know its an old question but for future references:

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

that should get you what you want..

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()));

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);

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