YII框架get,post传参
来源:互联网 发布:香烟真假软件 编辑:程序博客网 时间:2024/06/07 01:35
yii中
通过request组件来获取get,post参数
获取get传参:
$request = \Yii::$app->request;echo $request->get('id');
$request = \Yii::$app->request;$id = $request->get('id',1);// 1为不传参数默认为1
获取post传参:
$request = \Yii::$app->request;echo $request->post('username');
$request = \Yii::$app->request;$id = $request->post('username',‘tom);// tom为不传参数默认为tom
$request->isGet; 判断是否是get请求
$request->isPost; 判断是否是Post请求
返回 布尔值 true false
获取用户当前的ip地址
$request->userIP;
Controller返回数据到View(必须为数组格式 否则会报错)
$request = \Yii::$app->request;$userIP = $request->userIP;return $this->renderPartial('index',['userIP'=>$userIP]);
当我们返回数据的时候,通常会返回数组类型的数据
例:(1)
Controller:当我们返回多个数组时,我们要把各个数组放到定义好的大数组里 返回到View层
public function actionIndex (){$request = \Yii::$app->request;$user = ['username' => 'tom','age' => 19,];$article = ['title' => 'php是世界上最好的语言!'];$data = ['user' => $user,'article' => $article,];return $this->renderPartial('index',$data);}
View:来渲染Controller返回的数据, 有2种方法渲染(Yii框架没有模板引擎)
①:<?php echo 数据; ?>
②:<?= 数据; ?> //就是简化操作
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><body><h1><?php echo $user['username']; ?></h1><h2><?php echo $user['age']; ?></h2><h3><?= $article['title']; ?></h3></body></html>
(2)也可以用php的compact()函数,来返回多个数组。如果不了解这个函数请复制下面的网址
https://jingyan.baidu.com/article/fcb5aff789aa4fedaa4a7106.html
return $this->renderPartial('index',compact('user','article'));
防止xss攻击:
public function actionIndex (){$request = \Yii::$app->request;$data = ['tr'=>'hello world <script>alert(1)</script>',];return $this->renderPartial('index',$data);}
前台渲染数据时 会一直弹出框,这样是很危险的有两种办法来防止xss攻击
<?phpuse \yii\helpers\Html;use \yii\helpers\HtmlPurifier;?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><body><h3><?= Html::encode($tr); ?></h3><h3><?= HtmlPurifier::process($tr); ?></h3></body></html>
Html::encode() 转义
HtmlPurifier::process() 过滤
分配模板 renderPartial 和 render 的区别
renderPartial:自定义分配模板
render:分配的模板文件具有yii头部与脚步信息(父模板的信息)
阅读全文
0 0
- YII框架get,post传参
- php yii框架 post csrf
- yii框架 表单get传值
- Volley框架GET POST请求
- http get post传参
- Android开发,post、get请求,线程框架
- Android-异步HttpClient框架(get/post)
- CI框架获取post和get参数
- YII框架POST数据时400错误(张高伟)
- Yii框架
- YII框架
- YII 框架
- YII框架
- yii框架
- YII框架
- yii框架
- Yii 框架
- yii 框架
- java 链接neo4j数据库方法
- readv函数与sendv函数即相关缓冲分析
- grep命令与正则表达式
- JS表单验证手机号
- Java8系列之重新认识HashMap
- YII框架get,post传参
- linux字符设备(三)
- 浅析 OkHttp 的 TLS 连接过程
- spring boot中mybatis配置
- redis数据丢失问题的分析
- Mysql net start mysql启动,提示发生系统错误 5 拒绝访问
- Git基础使用
- mybatis 查询数据除了id正常外其余全为null
- L1-011. A-B