YII框架学习笔记之命名空间、操作响应与视图操作示例

(编辑:jimmy 日期: 2024/10/4 浏览:2)

本文实例讲述了YII框架命名空间、操作响应与视图操作。分享给大家供大家参考,具体如下:

YII基础准备

1.命名空间

<"htmlcode">
<"this is get method";
    }
    echo $request->userIP;//获取用户IP
    $res = \YII::$app->response;//获取响应状态
    $res->statusCode = 404;//人为设置响应状态码
    //$res->headers->add('pragma','no-cache');//设置head不设置缓存
    $res->headers->set('pragma','max-age=5');//设置head缓存5分?秒钟
    $res->headers->remove('pragma');
    //跳转
    $res->headers->add("location","http://www.baidu.com");
    $this->redirect("http://www.baidu.com",302);
    //文件下载
    $res->headers->add('content-disposition','attachment;filename="a.jpg"');
    $res->sendFile("robots.txt");
  }
}

3. Yii视图操作

<"hello God!";
$data = array();
$data["view_hello_str"] = $hello_str;
return $this->renderPartial("index",$data);
}
}
"htmlcode">
$helper_str = "hello world!<script>console.log(111);</script>"

<"htmlcode">
public $layout=false/'layout'

控制器成员方法内控制:

$this->layout=false/'layout'

视图中选择布局:

$this->context->layout=false/'layout'

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。