Yii Framework是一个基于组件、用于开发大型 Web 应用的高性能 PHP 框架。Yii提供了今日Web 2.0应用开发所须要的几乎一切功能。Yii是最有效率的PHP框架之一。最近我在看yii的使用,把记录留下来,但愿能帮助到有须要的朋友。php
## 标题获取http请求和设置http响应##
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/11/27 * Source:http://www.ruanpower.com * Time: 16:41 */ namespace app\controllers; use yii\web\Controller; class HelloController extends Controller { public function actionIndex() { #获取http请求 $request = \YII::$app->request; $id = $request->get('id', 50);#get $id = $request->post('id', 50);#post #设置http响应 $response = \Yii::$app->response; $response->statusCode = 404;#设置状态码 $response->headers->add('aaaaa', 'hhhhhhhhhhhhhhhhhhh'); #添加自定义响应 $response->headers->set('aaaaa', '55555555555555555'); #设置自定义响应 $response->headers->remove('aaaaa'); #删除自定义响应 #http跳转 $response->headers->add('location', 'http://www.ruanpower.com'); $this->redirect('http://www.ruanpower.com', 302); #yii框架自带跳转方法 #文件下载 $response->headers->add('content-disposition', 'attachment; filename="a.jpg"'); $response->sendFile('./robots.txt');#yii框架自带下载文件方法 } }
## session使用和cookie使用 ##html
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/11/27 * Source:http://www.ruanpower.com * Time: 16:41 */ namespace app\controllers; use yii\web\Controller; use yii\web\Cookie; class HelloController extends Controller { public function actionSession() { #YII框架之控制器session组件 $session = \Yii::$app->session; $session->open(); if (!$session->isActive) { echo 'session没有开启'; } $session->set('user', '傅荣'); #获取session $session['user']; $session->get('user');#效果同上 $session->remove('user'); #YII框架之控制器cookie组件 $cookie=\Yii::$app->response->cookies; $data= array( 'name'=>'user', 'value'=>'傅荣', ); $cookie->add(new Cookie($data)); $cookie->remove('user'); #删除 #获取cookie $request = \Yii::$app->request->cookies; echo $request->getValue('user',200); } }
## 视图使用 控制器代码 ##web
<?php /** * Created by PhpStorm. * User: CPR137 * Date: 2016/11/28 * Source:http://www.ruanpower.com * Time: 11:55 */ namespace app\controllers; use yii\web\Controller; class HelloController extends Controller { public function actionIndex(){ #yii视图使用 $testArr = array(1,2); $hello = 'hello world <script>alert(11111111)</script>'; $data=array( 'hello'=>$hello, 'testArr'=>$testArr, ); return $this->renderPartial('index',$data); } }
## 视图使用 模版代码 ##cookie
<html> <head lang="en"> <meta charset="UTF-8"> <title>index</title> </head> <body> <h1><?=$hello?></h1> <!--html转义js输出--> <h1><?=\yii\helpers\Html::encode($hello)?></h1> <!--html过滤js输出--> <h1><?=\yii\helpers\HtmlPurifier::process($hello)?></h1> <p><?=$testArr[1]?></p> <!DOCTYPE html> hello index </body> </html>
文章连接:http://www.ruanpower.com/?m=B...
文章来源:软炬博客session