Yii - CHttpRequest - 处理请求

————在Yii中我们可以使用PHP超级全局变量像$_SERVER,$_GET或者$_POST来直接响应请求数据,但是是更好的方法是使用Yii强大的 CHttpRequest 类,它解决了在各种不同服务器中的不一致性,管理cookies,提供一些额外的安全性和一套给力的面向对象方法。


我们可以使用Yii::app()->getRequest()来请求 CHttpRequest 组件。


—下面是 CHttpRequest 对 URL 操作的一些方法。在下面的表格中,我们用粗体字样标记了返回的部分。


getUrl http://yiibook.local/test/index?var=val
getHostInfo http://yiibook.local/test/index?var=val
getPathInfo http://yiibook.local/test/index?var=val
getRequestUri http://yiibook.local/test/index?var=val
getQueryString http://yiibook.local/test/index?var=val


—CHttpRequest确保请求类型的方法

                                                                  ——getIsPostRequest——返回是否这是一个POST请求。

                                                                  ——getIsAjaxRequest——返回这是否是一个AJAX请求。

                                                                  ——getRequestType——返回请求类型,比如 GET  POST HEAD PUT  DELETE.。

例如,我们可以使用 getIsAjaxRequest 来根据请求类型响应不同的内容:



class TestController extends CController
{
    public function actionIndex()
    {
        if(Yii::app()->request->isAjaxRequest) //等价于<strong>Yii::app()->getRequest()</strong>
            $this->renderPartial('test');
        else
            $this->render('test');
    }
}


在上面的代码中,如果请求是通过AJAX,我们就渲染了一个没有布局的视图。


—尽管PHP为POST和GET都提供了超全局变量,但 CHttpRequest 的对应方法则允许我们省略一些额外的检查:


                 ——getParam——返回指定GET或POST参数值。(public mixed getParam(string $name, mixed $defaultValue=NULL)

                 ——getQuery——返回指定GET参数值。(getQuery(string $name, mixed $defaultValue=NULL)

                 ——getPost——返回指定POST参数值。(string $name, mixed $defaultValue=NULL)


上面的 defaultValue 在默认参数值不存在时生效。下面是个简单的例子:



class TestController extends CController
{
    public function actionIndex()
    {
        $request = Yii::app()->request;
        
        $param = $request->getParam('id', 1);
        // 等价于
        $param = isset($_REQUEST['id']) ? $_REQUEST['id'] : 1;
        
        $param = $request->getQuery('id');
        // 等价于
        $param = isset($_GET['id']) ? $_GET['id'] : null;
        
        $param = $request->getPost('id', 1);
        // 等价于
        $param = isset($_POST['id']) ? $_POST['id'] : 1;
    }
}


——延伸一下:sendfile()方法。向用户发送一个文件。


class TestController extends CController
{
    public function actionIndex()
    {
        $request = Yii::app()->getRequest();
        $request->sendFile('test.txt', 'File content goes here.');
    }
}

——最后要看一下 getCookies 方法。它返回一个CCookieCollection类实例,这个实例允许我们处理 cookies。因为CCookieCollection继承自CMap,所以我们可以使用一些原始的PHP方法:



class TestController extends CController
{
    public function actionIndex()
    {
        $request = Yii::app()->request;
        // 获取cookie
        $cookie = $request->cookies['test'];
        if($cookie)
            // 打印 cookie 值
            echo $cookie->value;
        else {
            // 创建一个cookie实例
            $cookie=new CHttpCookie('test','I am a cookie!');
            $request->cookies['test'] = $cookie;
        }
    }
}


如果你有很多cookie值要处理,并且想精简代码,你可以使用像这样的助手方法:



class Cookie
{
    public static function get($name)
    {
        $cookie=Yii::app()->request->cookies[$name];
        if(!$cookie)
            return null;
       return $cookie->value;
    }
    
    public static function set($name, $value, $expiration=0)
    {
        $cookie=new CHttpCookie($name,$value);
        $cookie->expire = $expiration;
        Yii::app()->request->cookies[$name]=$cookie;
    }
}



在你把这份代码放到 protected/components/Cookie.php 后,我们就可以这样做:



class TestController extends CController
{
    public function actionIndex()
    {
        $cookie = Cookie::get('test');
        if($cookie)
            echo $cookie;
        else
            Cookie::set('test','I am a cookie!!');
    }
}






















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章