YII get post

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

普通的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,
];

相關文章

程式語言 最新文章