如何在zend中定义当前url(框架内部作业)

OTA*_*TAR 3 php zend-framework

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

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

Mar*_*vac 6

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

在视图脚本中使用:$this->url()获取当前URL.

或者通过实例使用via static integrated Zend Controller front:

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

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

$request = Zend_Controller_Front::getInstance()->getRequest();
$url = $request->getScheme() . '://' . $request->getHttpHost();
Run Code Online (Sandbox Code Playgroud)

在视图上使用它:

echo $this->serverUrl(true); # return with controller, action,...
Run Code Online (Sandbox Code Playgroud)

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

echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
Run Code Online (Sandbox Code Playgroud)

而不是这个例子在视图上使用:

$uri = $this->getRequest()->getHttpHost() . $this->view->url();
Run Code Online (Sandbox Code Playgroud)

如果您想在ZEND中使用getRequest,请更多地探索请求对象.

跳过它(自动示例如何工作).

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

首先,如果uri为null,则如果从IIS集中请​​求为as,则为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;
Run Code Online (Sandbox Code Playgroud)