Yii 之控制器响应

    public function actionIndex(){
        //控制器响应处理
        $res = \Yii::$app->response;

        //设置状态码
//        $res->statusCode = '404';

        //设置header头
//        $res->headers->add('pragma','no-cache');//增加
//        $res->headers->set('pragma','max-age=5');//修改
//        $res->headers->remove('pragma');//删除

        //跳转
        //方法一
//        $res->headers->add('location','http://www.baidu.com');
        //方法二
//        $this->redirect('http://www.baidu.com');

        //文件下载
        //方法一
//        $res->headers->add('content-disposition','attachment;filename=a.jpg');
         //方法二
//        $res->sendFile('./robots.txt');

    }

猜你喜欢

转载自www.cnblogs.com/gyfluck/p/9090036.html