gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:34:21 25 4
gpt4 key购买 nike

请问什么脚本使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com