YII get post

普通的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,
];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值