The web Request class represents an HTTP request

It encapsulates the $_SERVER variable and resolves its inconsistency among different Web servers.

Also it provides an interface to retrieve request parameters from $_POST, $_GET, $_COOKIES and REST parameters sent via other HTTP methods like PUT or DELETE.

HTTP Headers

// $headers is an object of yii\web\HeaderCollection
$headers = Yii::$app->request->headers;

$accept = $headers->get('Accept');

if( $headers->has('User-Agent') ) { /* there is User-Agent header */ }

Client Information

获取用户 IP:

$userHost = Yii::$app->request->userHost;
$ip = Yii::$app->getRequest()->getUserIP();



 * Regenerates CSRF token
 * @since
protected function regenerateCsrfToken()
    $request = Yii::$app->getRequest();
    if ($request->enableCsrfCookie || $this->enableSession) {