YII get post

慕容烈
• 阅读 2267

普通的get和pst请求

$request = Yii::$app->request;
$get = $request->get(); 
// equivalent to: $get = $_GET;
$id = $request->get('id');   
// equivalent to: $id = isset($_GET['id']) ? $_GET['id'] : null;
$id = $request->get('id', 1);   
// equivalent to: $id = isset($_GET['id']) ? $_GET['id'] : 1;
//添加了默认值 
$post = $request->post(); 
// equivalent to: $post = $_POST; 
$name = $request->post('name');   
// equivalent to: $name = isset($_POST['name']) ? $_POST['name'] : null; 
$name = $request->post('name', '');   
// equivalent to: $name = isset($_POST['name']) ? $_POST['name'] : '';
//添加了默认值

判断请求属性**

$request = Yii::$app->request;
if ($request->isAjax) { // 判断是否为AJAX 请求 }
if ($request->isGet)  { // 判断是否为GET 请求 }
if ($request->isPost) { // 判断是否为POST 请求}
if ($request->isPut)  { // 判断是否为PUT 请求 }
if ($request->isSecureConnection) { // 判断是否为https 请求}

获取请求头信息

// $headers is an object of yii\web\HeaderCollection 
$headers = Yii::$app->request->headers;
// 返回header头部所有信息
 
$accept = $headers->get('Accept');
if ($headers->has('User-Agent')) { // 获取User-Agent }

获取用户客户端信息

$userHost = Yii::$app->request->userHost; 
$userIP = Yii::$app->request->userIP;

2.common main.php 配置文件

<?php
return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'timeZone'=>'Asia/shanghai',
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=139.196.203.167;dbname=zb2_zhibo',
            'username' => 'root',
            'password' => 'qizheng=-/110',
            'charset' => 'utf8',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],

//        # 缓存组件
//        'cache'=>array(
//            'class'=>'yii\caching\MemCache',
//            'servers'=>array(
//                array(
//                    'host'=>'127.0.0.1',
//                    'port'=>11211,
//                    'weight'=>60,
//                ),
////                array(
////                    'host'=>'server2',
////                    'port'=>11211,
////                    'weight'=>40,
////                ),
//            )
//        ),

        # 短信组件
        'hsms' => [
            'class' => 'yii\hsms\Hsms',
            'url'=>'http://sms.upapp.net:3001/api/9fad341d0aa3a99b2762eba0183dd0a55ecfaccd/sms/submit/',
        ],
        # 七牛云组件
        'qiniu' => [
            'class' => 'yii\qiniu\Qiniu',
        ],
    ],
];

3.前台配置文件

<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/params.php')
);

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [
        'request' => [
            'enableCookieValidation' => true,
            'cookieValidationKey' => 'cookie_jt018_key',
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],

        # URL美化
        'urlManager' => [
            'enablePrettyUrl' => false,  // 启用路由
            'showScriptName' => false,  // 隐藏index
            //'suffix'=>'.html',        // 后缀
            'rules' => [
                'posts'=>'site/index',
//                'posts/<id:\d+>/<page:\d{0,3}>'=>'site/index',
            ],
        ],
    ],
    'params' => $params,
];
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
GET请求中文乱码
今天碰到一个问题,就是GET请求接收到的数据中中文总是乱码web.xml配置<!过滤字符集<filter<filternameencoding</filtername<filterclassorg.springframework.w
Wesley13 Wesley13
3年前
PHP CURL方法,GET&POST请求。
//get获取function ihttp_get($url) {return ihttp_request($url);}//post获取function ihttp_post($url, $data) {$headers  array('ContentType'  '
Stella981 Stella981
3年前
SpringBoot HTTP接口 GET请求
1\.普通传参@RequestMapping(path"/{city_id}/{user_id}",methodRequestMethod.GET)publicObjectfindUser(@PathVariable("city_id")StringcityId,
Stella981 Stella981
3年前
GitHub Actions
使用GitHubActions发布版本时,获取触发的tag作为发布的版本号.方式一通过step获取tag,在需要使用的地方使用steps.get_version.outputs.VERSION,其中get_version是step的id.name:Release
Wesley13 Wesley13
3年前
Java 通过get post 请求url
1️⃣.已获取小程序的access\_token为例,通过Get请求url1importcom.alibaba.fastjson.JSONObject;23StringwechatUrl"https://api.weixin.qq.com/cgibin/token?grant_typeclie
Stella981 Stella981
3年前
From表单提交的几种方式
<body<formaction"https://my.oschina.net/u/3285916"method"get"name"formOne"id"formId"name:<inputtype"text"name"name"pwd:<inputtyp
Stella981 Stella981
3年前
Ajax_请求get,post案例
1\.最原始的ajax请求方式(1).get请求<%@PageLanguage"C"AutoEventWireup"true"CodeFile"AjaxDemo.aspx.cs"Inherits"ajax_AjaxDemo"%<!DOCTYPEhtml<htmlxmln
Stella981 Stella981
3年前
Hibernate中get()和load()的区别
Hibernate中根据Id单条查询获取对象的方式有两种,分别是get()和load(),来看一下这两种方式的区别。1\.get()使用get()来根据ID进行单条查询:Userusersession.get(User.class,"1");当get()方法被调用的时候就会立即发出SQL语句:Hiberna
Stella981 Stella981
3年前
Django之CBV装饰器,跨站请求伪造,auth认证
CBV加装饰器基于session实现登录deflogin(request):ifrequest.method'POST':usernamerequest.POST.get('username')pwdrequest.POST.get(
liam liam
3年前
post接口请求测试,通俗易懂!
GET方法和POST方法传递数据的异同http请求方法get和post是最常被用到的两个方法,get常用于向服务器请求数据,post常用于提交数据给服务器处理。GET方法其实也可以传递少量的数据。但它存在以下问题:1)GET方法不包含body,因此以在URL中拼接字段的方式传递数据,2)GET方法中的URL参数会被显示到地
飞鹅官方账号 飞鹅官方账号
11个月前
五、飞鹅官网API接口文档
接口列表1.获取网站信息请求方法:GET请求URL:/api/site/getSiteInfo请求参数无返回结果json"code":1,"data":"id":1,//id"title":"SampleSiteName",//网站名称"intro":"T