`

Yii分析6:CHttpRequest

 
阅读更多

官方说明:CHttpRequest封装了$_SERVER变量,同时解决了不同web服务器之间一致性的问题。

/**
     * 返回当前请求的路径.
     * 头尾的斜线会被去除掉
     * 注意从1.1.4开始pathinfo被编码
     * 在1.1.4之前, 是否被编码取决于服务器
     * (大部分情况未编码).
     */
    public function getPathInfo()
    {  
        if($this->_pathInfo===null)
        {  
            $pathInfo=$this->getRequestUri();

            if(($pos=strpos($pathInfo,'?'))!==false)
               $pathInfo=substr($pathInfo,0,$pos);

            $pathInfo=urldecode($pathInfo);

            $scriptUrl=$this->getScriptUrl();
            $baseUrl=$this->getBaseUrl();
            if(strpos($pathInfo,$scriptUrl)===0)
                $pathInfo=substr($pathInfo,strlen($scriptUrl));
            else if($baseUrl==='' || strpos($pathInfo,$baseUrl)===0)
                $pathInfo=substr($pathInfo,strlen($baseUrl));
            else if(strpos($_SERVER['PHP_SELF'],$scriptUrl)===0)
                $pathInfo=substr($_SERVER['PHP_SELF'],strlen($scriptUrl));
            else
                throw new CException(Yii::t('yii','CHttpRequest is unable to determine the path info of the request.'));

            $this->_pathInfo=trim($pathInfo,'/');
        }  
        return $this->_pathInfo;
}
/**
     * 返回请求URI
     * 指的是跟在host info后面的部分
     * 包含了 query string
     * 这个方法的实现参考了Zend框架中的Zend_Controller_Request_Http.
     * @return string the request URI portion for the currently requested URL.
     * @throws CException if the request URI cannot be determined due to improper server configuration
     * @since 1.0.1
     */
    public function getRequestUri()
    {
        if($this->_requestUri===null)
        {
            if(isset($_SERVER['HTTP_X_REWRITE_URL'])) // IIS
                $this->_requestUri=$_SERVER['HTTP_X_REWRITE_URL'];
            else if(isset($_SERVER['REQUEST_URI']))
            {
                $this->_requestUri=$_SERVER['REQUEST_URI'];
                if(isset($_SERVER['HTTP_HOST']))//来自header
                {
                    if(strpos($this->_requestUri,$_SERVER['HTTP_HOST'])!==false)
                        $this->_requestUri=preg_replace('/^\w+:\/\/[^\/]+/','',$this->_requestUri);
                }
                else
                    $this->_requestUri=preg_replace('/^(http|https):\/\/[^\/]+/i','',$this->_requestUri);
            }
            else if(isset($_SERVER['ORIG_PATH_INFO']))  // IIS 5.0 CGI
            {
                $this->_requestUri=$_SERVER['ORIG_PATH_INFO'];
                if(!empty($_SERVER['QUERY_STRING']))
                    $this->_requestUri.='?'.$_SERVER['QUERY_STRING'];
            }
            else
                throw new CException(Yii::t('yii','CHttpRequest is unable to determine the request URI.'));
        }

        return $this->_requestUri;
    }
 
1
1
分享到:
评论

相关推荐

    YII Framework学习之request与response用法(基于CHttpRequest响应)

    在YII Framework中,`request`与`response`是...理解并熟练使用`CHttpRequest`,对于开发高效且安全的YII应用程序至关重要。在实际项目中,可以根据需求自定义请求验证规则,或扩展`CHttpRequest`以满足更复杂的需求。

    Yii框架执行流程及部分源码分析

    6. **Yii核心参考** Yii框架的核心组件还包括响应(`CResponse`)、用户会话(`CWebSession`)、缓存(`CCache`)、数据库访问(`CDbConnection`)等。这些组件通过依赖注入(Dependency Injection)和组件注册...

    PHP开发框架Yii全方位教程(1)应用(Yiiapp).pdf

    Yii 应用程序的核心概念是“应用”(Yii::app),它作为整个请求处理流程的中心,负责接收用户请求,并将其分派给合适的控制器处理。在Yii框架中,应用同时扮演着存储全局配置的角色。 应用在入口脚本中以单例模式...

    YII Framework框架教程之安全方案详解

    启用CSRF防护,需在应用配置中设置`CHttpRequest`组件: ```php return array( 'components' => array( 'request' => array( 'enableCsrfValidation' => true, ), ), ); ``` 使用`CHtml::form`生成表单,...

Global site tag (gtag.js) - Google Analytics