zend-framework - 如何在 zend 中定义当前 url(框架的内部工作)

标签 zend-framework php

请问什么脚本使用zend框架来定义当前的URL?更确切地说,我对使用 ZEND 定义域名感兴趣:这个 $_SERVER['HTTP_HOST'] 或者这个 $_SERVER['SERVER_NAME'] ? (或者可能是其他东西)?

附言(我在文档中搜索但没有找到,(我不知道这个框架),我也在谷歌搜索,但也没有找到我的问题的答案?)

最佳答案

尝试使用:$this->getRequest()->getRequestUri() 获取当前请求的 URI。

在 View 脚本中使用:$this->url() 获取当前 URL。

或者通过静态集成 Zend Controller front via instance 使用:

$uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();

可以通过singleton获取一个URI的值实现来获取一个request()数据的值:

$request = Zend_Controller_Front::getInstance()->getRequest();
$url = $request->getScheme() . '://' . $request->getHttpHost();

在 View 上使用它作为:

echo $this->serverUrl(true); # return with controller, action,...

你应该避免硬编码,例如示例(不要使用!):

echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];

在 View 上使用 as 代替此示例:

$uri = $this->getRequest()->getHttpHost() . $this->view->url();

如果您想在 ZEND 中使用 getRequest 更多探索 The Request Object .

跳过下面的内容(尸检示例是如何工作的)。

完整的示例代码 getRequestUri() 如何工作以及为什么 isRequest 而不是使用 $_SERVER 是因为在特定平台上随机获取数据:

首先如果 uri 为空,然后如果从 IIS 请求设置为 HTTP_X_REWRITE_URL。如果不是,请检查 IIS7 重写的 uri(包括编码的 uri)。如果不在 IIS 上,则 REQUEST_URI 将检查 HTTP_HOSTNAME 的方案,或者如果失败则用作 ORIG_PATH_INFO 并获取 QUERY_STRING。

如果设置了,则通过类中返回对象$this的字符串自动抓取数据。

如果失败,将设置一个已解析的字符串而不是设置它。

if ($requestUri === null) {
        if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
            $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
        } elseif (
            // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
            isset($_SERVER['IIS_WasUrlRewritten'])
            && $_SERVER['IIS_WasUrlRewritten'] == '1'
            && isset($_SERVER['UNENCODED_URL'])
            && $_SERVER['UNENCODED_URL'] != ''
            ) {
            $requestUri = $_SERVER['UNENCODED_URL'];
        } elseif (isset($_SERVER['REQUEST_URI'])) {
            $requestUri = $_SERVER['REQUEST_URI'];
            // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
            $schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost();
            if (strpos($requestUri, $schemeAndHttpHost) === 0) {
                $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
            }
        } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
            $requestUri = $_SERVER['ORIG_PATH_INFO'];
            if (!empty($_SERVER['QUERY_STRING'])) {
                $requestUri .= '?' . $_SERVER['QUERY_STRING'];
            }
        } else {
            return $this;
        }
    } elseif (!is_string($requestUri)) {
        return $this;
    } else {
        // Set GET items, if available
        if (false !== ($pos = strpos($requestUri, '?'))) {
            // Get key => value pairs and set $_GET
            $query = substr($requestUri, $pos + 1);
            parse_str($query, $vars);
            $this->setQuery($vars);
        }
    }

    $this->_requestUri = $requestUri;
    return $this;

关于zend-framework - 如何在 zend 中定义当前 url(框架的内部工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12992547/

相关文章:

php - 在 zend 框架中的特定列中显示数组数据

php - 禅德 : Execute something before a controller is executed

javascript - 在提交之前使用 Ajax 上传文件的最佳方法

php - MySQL 中日期的混合排序

PHP Zend 日期格式

zend-framework - Zend 框架 : How to render a different action?

php - 在循环中包含一个变量文件名

php - Drupal 8.3 自定义 Rest POST 错误 BadRequestHttpException : The type link relation must be specified

php - Magento 中过滤器搜索时的自定义网格中未返回产品名称

php - Laravel 重定向到区域设置更改的路由