解决lnmp环境不支持PATH_INFO问题

之前一篇文章介绍过关于 ningx 环境支持path_info的介绍, 最近在研究YII2框架发现环境不支持path_info竟然也能正常访问,看来自己真的太low了, 果断xdebug模式开始进行调试。

发现YII2下面的一段代码如下:


/** * Resolves the request URI portion for the currently requested URL. * This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any. * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. * @return string|boolean the request URI portion for the currently requested URL. * Note that the URI returned is URL-encoded. * @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration */ protected function resolveRequestUri() { if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // IIS $requestUri = $_SERVER['HTTP_X_REWRITE_URL']; } elseif (isset($_SERVER['REQUEST_URI'])) { $requestUri = $_SERVER['REQUEST_URI']; if ($requestUri !== '' && $requestUri[0] !== '/') { $requestUri = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $requestUri); } } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 CGI $requestUri = $_SERVER['ORIG_PATH_INFO']; if (!empty($_SERVER['QUERY_STRING'])) { $requestUri .= '?' . $_SERVER['QUERY_STRING']; } } else { throw new InvalidConfigException('Unable to determine the request URI.'); } return $requestUri; }

以上代码有一段是判断REQUEST_URI来进行实现。

如果你的环境不支持path_info,而你对于配置nginx又不是很在行,可以添加如下代码在首页入口。


if ((!isset($_SERVER['PATH_INFO']) || !$_SERVER['PATH_INFO']) && isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] ) { $_SERVER['PATH_INFO'] = preg_replace('/^(http|https):\/\/[^\/]+/i', '', $_SERVER['REQUEST_URI']); }

本人在thinkphp5中测试以上代码可以正常进行,如果大家使用过程中遇到问题可以联系我!

本文已经被查看331次

发表评论

电子邮件地址不会被公开。 必填项已用*标注