Use of Yii2 request

WBOY
Release: 2016-07-29 08:58:19
Original
911 people have browsed it

Ordinary get and pst requests

<code>$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'] : '';
//添加了默认值</code>
Copy after login

Judge request attributes

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

Get request header information

<code>// $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 }</code>
Copy after login

Get user client information

<code>$userHost = Yii::$app->request->userHost; 
$userIP = Yii::$app->request->userIP;</code>
Copy after login

The above introduces the use of Yii2 request, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!