Laravel5.5 source code Detailed--How is the request generated?

Source: Internet
Author: User
Tags http request sapi
Laravel5.5 Source code detailed –request is how to generate.

In the Laravel launch page, which is the public/index.php file, there is a sentence

$response = $kernel->handle (
    $request = Illuminate\http\request::capture ()
);

It creates a Illuminate\http\request instance based on the Http request that the browser has passed in. Next, let's look at the generation of this instance.

$request illuminate\http\request::capture ()

public static function capture ()
{
    static::enablehttpmethodparameteroverride ();
    Return Static::createfrombase (Symfonyrequest::createfromglobals ());
}

This involves three functions, the 1th and 2nd functions of A. Request::capture () Call

When you create a request, the related functions that are called are as follows

Vendor\symfony\http-foundation\request

public static function Enablehttpmethodparameteroverride () {self:: $httpMethodParameterOverride = true;}  public static function Createfromglobals () {//With the PHP ' s bug #66606, the php ' s built-in Web server//stores
    The Content-type and Content-length header values in//Http_content_type and Http_content_length fields.
    The official notes above indicate that there should be http_content_type and http_content_length, but no debugging found.
    $server = $_server; if (' cli-server ' = = Php_sapi) {if (array_key_exists (' http_content_length ', $_server)) {$server [' CON
        Tent_length '] = $_server[' http_content_length ']; } if (Array_key_exists (' Http_content_type ', $_server)) {$server [' content_type '] = $_server[' Http_cont
        Ent_type ']; }//Generate the request instance $request = Self::createrequestfromfactory ($_get, $_post, Array (), $_cookie, $_files, $serv

    ER);
   if (0 = = Strpos ($request->headers->get (' Content_Type '), ' application/x-www-form-urlencoded ')     && In_array (Strtoupper ($request->server->get (' Request_method ', ' get ')), array (' Put ', ' DELETE ', '
        PATCH ')) {parse_str ($request->getcontent (), $data);
    $request->request = new Parameterbag ($data);
return $request; The private static function createrequestfromfactory (array $query = Array (), array $request = Array (), array $attributes = Array (), array $cookies = Array (), array $files = Array (), array $server = Array (), $content = null) {if (Self:: $requ estfactory) {$request = Call_user_func (self:: $requestFactory, $query, $request, $attributes, $cookies, $files, $s

        Erver, $content); if (! $request instanceof Self) {throw new \logicexception (' The Request factory must return an instance of Sym
        Fony\component\httpfoundation\request. ');
    return $request;
return new Static ($query, $request, $attributes, $cookies, $files, $server, $content); }

The last sentence is return new static ($query, $request, $attributes, $cookies, $files, $server, $content), and generates an instance of the request, namely symfony\ Component\httpfoundation\request.

Note that the namespace of symfony\http-foundation\request is symfony\component\httpfoundation; Description One: a) $_server is an array that records the information on the server, which takes effect at the time of startup,

array:24 ["Document_root" => "D:\wamp64\www\laravel\laraveltest\public" "REMOTE_ADDR" => "127.0.0.1" "REMOTE _port "=>" 12492 "Server_software" => "PHP 7.1.9 Development SERVER" "Server_protocol" => "http/1.1" "Serv" Er_name "=>" 127.0.0.1 "Server_port" => "8000" "Request_uri" => "/user/image/avatarupload" "Request_method" "=>" Get "" Script_name "=>"/index.php "" Script_filename "=>" D:\wamp64\www\laravel\laraveltest\public\index "  . php "Path_info" => "/user/image/avatarupload" "Php_self" => "/index.php/user/image/avatarupload" "HTTP_HOST" => "localhost:8000" "Http_connection" => "keep-alive" "Http_cache_control" => "max-age=0" "HTTP_USER_AGENT" "=>" mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/63.0.3239.84 safari/537.36 "" http_upgrade_insecure_requests "= > "1" "Http_accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*";q=0.8 "" Http_accept_encoding "=>" gzip, deflate, BR "Http_accept_language" => "zh-cn,zh;q=0.9" "Http_cookie" => "remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d= Eyjpdii6imdjrxdtzlnbbet3a1pkm3p0owxmtve9psisinzhbhvlijoiejzgdnz2qlfdy0djqun5bxd5egpuvhgyzfuztkv5ztzqrvjcve▶ "" Request_time_float "=> 1514626260.1018" Request_time "=> 1514626260]
b) Php_sapi = ' Cli-server ',

Reference, http://php.net/manual/zh/features.commandline.php

PHP can be applied to the terminal or to the Web server; The SAPI applied to the terminal is called the CLI SAPI, and the Web server is called the CGI SAPI, which is installed under Windows. The corresponding distinctions are php.exe and php-cgi.exe.

In PHP, How do you know which SAPI you are using?

At the command line, running php-v can tell if the PHP is CGI or CLI. Refer to the function php_sapi_name () and constant Php_sapi. c) $_cookie

In particular: Cookies are a thing that the client stores data, and its role is that when a user logs on, subsequent requests do not need to be authenticated again. The reason is that the browser will bring a cookie value on the client's requested header, which indicates who the user is. Of course, the user does not register to login to the website, this cookie is also necessary, but there is no identity information.

So, when you open the browser, input www.mynetwork.com, the browser will first send a cookie to the server mynetwork.com Web site server, tell the server to browse the page, at this time, the server must respond and processing cookies (that is: $ _cookie).

The other, the original cookie for each browser is different, for example, IE11 is an empty string, Firefox is the following look,

Array:2 [
  "Bdshare_firstime" => "1511944517485"
  "Remember_web_59ba36addc2b2f9401580▶" => " Eyjpdii6infbsouzj▶ "
]
d) Other (incoming createrequestfromfactory) parameters

Currently these references are empty arrays [], as follows

$_get, $_post, Array (), $_files, $server
Description Two

About the cookie management class.

The Cookie management class Illuminate\cookie\cookiejar was created when Illuminate\cookie\cookieserviceprovider was registered.

<?php
namespace Illuminate\cookie;
Use Illuminate\support\serviceprovider;

Class Cookieserviceprovider extends serviceprovider
{public
    function register ()
    {
        $this->app- >singleton (' Cookie ', function ($app) {
            $config = $app->make (' config ')->get (' Session ');
            Return (new Cookiejar)->setdefaultpathanddomain (
                $config [' Path '], 
                $config [' domain '], 
                $config [' Secure '], 
                $config [' Same_site ']??? null
            );}
        )
    ;
}
B. The 3rd of the Request::capture () call
The public static function Createfrombase (Symfonyrequest $request)
{
    //first confirms that it is a illuminate\http\request instance, Just created here is
    //symfony\component\httpfoundation, so if judgment is false,
    if ($request instanceof static) {return
        $ request;
    }

    $content = $request->content;

    Because it is not a illuminate\http\request instance, this creates an instance of the request Class Laravel processing
    $request = (new static)->duplicate (
        $ Request->query->all (), $request->request->all (), $request->attributes->all (),
        $request- >cookies->all (), $request->files->all (), $request->server->all ()
    );

    $request->content = $content;

    There are no request parameters, if any, take it here (just generated is not, all from $_server and other parameters where to take)
    $request->request = $request->getinputsource ();

    return $request;
}

This is interesting, Laravel uses the symfony third party code, takes all the parameters of Symfony\component\httpfoundation object directly, takes advantage of $request = (new static)-> Duplicate (...) Cloning method, produced a illuminate\http\request.

At this point, a complete illuminate\http\request instance $request the request is generated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.