【问题标题】:Zend - Get everything after base Url (Controller, Action, and any params)Zend - 获取基本 URL 之后的所有内容(控制器、动作和任何参数)
【发布时间】:2011-01-01 08:29:08
【问题描述】:

我正在尝试使用 Zend 创建一个登录系统,当用户尝试访问受限页面时,如果他们未登录,他们将被带到登录页面。我遇到的问题是获取他们的 URL试图事先访问。是否有一个 Zend 函数会在基本 Url 之后返回所有内容?例如,我需要将“moduleName/controllerName/ActionName/param1/value1/param2/value2”等作为查询字符串参数发送到登录页面(例如:login/?redirect=controllerName/actionName/param1/value1/参数2/值)

我可以得到控制器和动作名称,我得到参数,但它已经包括模块、控制器和动作。我只想得到我需要的东西。我想出了很长的路要这样做:

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

但即便如此,我最终还是得到了我不想要的模块/管理员/控制器/索引/等参数。那么,在没有控制器和操作作为参数值的情况下,我如何才能将所有内容都作为字符串获取,就像它在 URL 中一样,或者至少只是字符串中的参数?

**编辑:这是我目前的解决方案,但必须有一种更优雅的方式来做到这一点**

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

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    我认为您可以像这样传递最后一个请求 URI:

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

    【讨论】:

    • 这似乎是一个不错的候选,但它也返回了 baseUrl。有什么方法可以让所有内容都以 /moduleName/controllerName/actionName/param1/value1/param2/value2 开头??
    • 要获取baseUrl 之后的所有内容,我手头没有解决方案。在我的情况下,登录后,我只需获取完整的 URI 并重定向到它,如下所示:$this-&gt;_redirect($full_url, array('prependBase'=&gt;false)); 这有帮助吗?
    • 嘿 Derek,我不知道 _redirect 使用 prependBase 选项采用了该选项数组。很高兴知道,谢谢。现在我已经完成了上面编辑中提到的所有设置,但可能会在某个时候切换到这个。谢谢。
    【解决方案2】:

    我知道这是一个老问题,但供将来参考:

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

    这应该可以得到你想要的..

    【讨论】:

      【解决方案3】:

      不知道为什么 Zend 没有这个功能,但这就是我实现它的方式。我的用例是我想将使用默认路由直接进入控制器的旧 URL 重定向到使用路由的新 API url 结构。

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

      【讨论】:

        【解决方案4】:

        老问题,但这是一个对我有用的解决方案。从这里你可能想对它进行 url 编码等

        $currentRoute = substr($this->getRequest()->getRequestUri(),strlen($this->getRequest()->getBaseUrl()));

        【讨论】:

          【解决方案5】:

          您可以尝试使用urlencode()urldecode() 对各个参数字符串进行编码/解码。一个例子是:

          // outputs %2Fpath%2Fto%2Ffile.php
          $path = '/path/to/file.php';
          echo urlencode($path);
          

          【讨论】:

          • 我正在寻找使用 Zend 框架的解决方案。我没有路径,我想获取原始请求中包含的所有参数
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多