zoukankan      html  css  js  c++  java
  • YII框架get,post传参

    https://blog.csdn.net/qq_37179628/article/details/75344959

    通过request组件来获取get,post参数

    1、获取get传参:

    $request = Yii::$app->request;
    echo $request->get('id');

    2、获取get传参给默认值:

    $request = Yii::$app->request;
    $id = $request->get('id',1);// 1为不传参数默认为1

    3、获取post传参:

    $request = Yii::$app->request;
    echo $request->post('username');

    4、获取post传参给默认值:

    $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攻击

    <?php
    use yiihelpersHtml;
    use yiihelpersHtmlPurifier;
    ?>

    <!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头部与脚步信息(父模板的信息)

  • 相关阅读:
    [NOI2019]回家路线(最短路,斜率优化)
    LOJ6686 Stupid GCD(数论,欧拉函数,杜教筛)
    Codeforces Global Round 4 题解
    CF908G New Year and Original Order(DP,数位 DP)
    [BJOI2019]光线(DP)
    CF1194F Crossword Expert(数论,组合数学)
    SPOJ31428 FIBONOMIAL(斐波那契数列)
    Codeforces Round 573 (Div.1) 题解
    [THUPC2018]弗雷兹的玩具商店(线段树,背包)
    数学基础
  • 原文地址:https://www.cnblogs.com/xieqijiang/p/14478990.html
Copyright © 2011-2022 走看看