This article mainly introduces about the Laravel core interpretation request, has a certain reference value, now share to everyone, the need for friends can refer to
Request
Many frameworks abstract requests from clients into classes for easy application use, no exception in Laravel. Illuminate\Http\Request
classes in the Laravel framework are abstractions of client requests, which are built on the request Symfony
component provided by the framework. Today's article is a brief look at how Laravel creates the request object, and about the ability of the request object to provide the application I will not be too much to say, after I finish the creation process, you will know where to find the source of the request object to provide the method, Some of the online quick check list lists some of the methods provided by the request, but not enough and there is no explanation, so I still recommend in the development if the curiosity request has achieved the ability you want to go to the source of the request to see if there is a corresponding method, The execution results of each method are clearly indicated in the method comments. Let's get down to the chase.
Create a Request object
We can see in the file of the Laravel application index.php
that the request object was created before the Laravel application was formally started:
Public/index.php$app = require_once __dir__. ' /.. /bootstrap/app.php '; $kernel = $app->make (illuminate\contracts\http\kernel::class); $response = $kernel Handle ( //Create Request object $request = Illuminate\http\request::capture ());
The client's HTTP request is the object of the Illuminate\Http\Request
class
Class Request extends Symfonyrequest implements Arrayable, arrayaccess{ //New Request instance public static function Capture () { static::enablehttpmethodparameteroverride (); Return Static::createfrombase (Symfonyrequest::createfromglobals ());} }
Through the Illuminate\Http\Request
source of the class can see that it is inherited from Symfony Request
the class, so many of the functions implemented in the class are based on the Illuminate\Http\Request
Symfony Reques
functionality provided to implement. The code above can see that the capture
method is also dependent on the instance of the class when it creates a new request object Symfony Request
.
namespace Symfony\component\httpfoundation;class request{/** * Create a Smyfony Request instance based on a super global array provided by PHP * * @ret Urn static */public static function Createfromglobals () {//With the PHP ' s bug #66606, the php ' s built- In Web server//stores the Content-type and Content-length headers values in//Http_content_type and Http_c Ontent_length fields. $server = $_server; if (' cli-server ' = = = = Php_sapi) {if (array_key_exists (' http_content_length ', $_server)) {$serve r[' content_length '] = $_server[' http_content_length ']; } if (Array_key_exists (' Http_content_type ', $_server)) {$server [' content_type '] = $_server[' HTT P_content_type ']; }} $request = Self::createrequestfromfactory ($_get, $_post, Array (), $_cookie, $_files, $server); if (0 = = Strpos ($request->headers->get (' Content_Type '), ' application/x-www-form-urlencoded ') &&amP 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; } }
There is an additional explanation for the above code, which starts with PHP's built-in Builtin Web server from PHP5.4, which can be started by a command-line interpreter, for example:
Php-s localhost:8000-t Htdocs
-S <addr>:<port> Run with built-in web server.-t <docroot> Specify document root <docroot> fo R built-in Web server.
However, there is a bug in the built-in Web server that will be stored in and between the CONTENT_LENGTH
CONTENT_TYPE
two request headers in HTTP_CONTENT_LENGTH
HTTP_CONTENT_TYPE
order to unify the request header fields in the built-in server and the real server, so special handling is done here.
The creation of the Symfony request instance is created by a super global array in PHP, which has,,, $_GET
$_POST
$_COOKIE
$_FILES
$_SERVER
covers all the Super global arrays in PHP that are related to HTTP requests. The ParamterBag
ServerBag
FileBag
HeaderBag
instances provided in the Symfony package are created based on these global arrays when the Symfony request instance is created. These are all symfony provide access and settings APIs for different HTTP components, and for ParamterBag
those who are interested in symfony, please go to the source code and see for yourself, there is not much to say.
Class request{/** * @param array $query the GET parameters * @param array $ Request the POST parameters * @param array $attributes the request attributes (parameters parsed fro M the Path_info, ...) * @param array $cookies the COOKIE parameters * @param array $files the files PA Rameters * @param array $server the server parameters * @param string|resource|null $content The raw body data * * Public function __construct (array $query = Array (), array $request = Array (), array $attribut es = Array (), array $cookies = Array (), array $files = Array (), array $server = Array (), $content = null) {$thi S->initialize ($query, $request, $attributes, $cookies, $files, $server, $content); The Public function initialize (array $query = Array (), array $request = Array (), array $attributes = Array (), array $ cookies = Array (), array $files = Array (), array $server = Array (), $content = null) {$this->request = new Parameterbag ($request); $this->query = new Parameterbag ($query); $this->attributes = new Parameterbag ($attributes); $this->cookies = new Parameterbag ($cookies); $this->files = new Filebag ($files); $this->server = new Serverbag ($server); $this->headers = new Headerbag ($this->server->getheaders ()); $this->content = $content; $this->languages = null; $this->charsets = null; $this->encodings = null; $this->acceptablecontenttypes = null; $this->pathinfo = null; $this->requesturi = null; $this->baseurl = null; $this->basepath = null; $this->method = null; $this->format = null; } }
You can see the Symfony request class in addition to the above mentioned, there are many properties, these properties together constitute a complete abstraction of the HTTP request, we can easily access through the instance properties Method
, Charset
such as the properties of these HTTP requests.
When you get the Symfony request instance, Laravel clones the instance and resets some of these properties:
namespace Illuminate\http;class Request extends .... {//Create a request instance on the basis of Symfony request instance public static function Createfrombase (Symfonyrequest $request) { if ($request instanceof static) {return $request; } $content = $request->content; $request = (new static)->duplicate ($request->query->all (), $request->request->all (), $request-& Gt;attributes->all (), $request->cookies->all (), $request->files->all (), $request->server-> All ()); $request->content = $content; $request->request = $request->getinputsource (); return $request; The Public function Duplicate (array $query = null, array $request = null, array $attributes = null, array $cookies = NULL, array $files = null, array $server = NULL) {return parent::d uplicate ($query, $request, $attributes, $cook IES, $this->filterfiles ($files), $server); }}//symfony RequestThe duplicate method in the Public function duplicate (array $query = null, array $request = null, array $attributes = NULL, array $c Ookies = null, array $files = null, array $server = null) {$dup = clone $this; if (null!== $query) {$dup->query = new Parameterbag ($query); } if (null!== $request) {$dup->request = new Parameterbag ($request); } if (null!== $attributes) {$dup->attributes = new Parameterbag ($attributes); } if (null!== $cookies) {$dup->cookies = new Parameterbag ($cookies); } if (null!== $files) {$dup->files = new Filebag ($files); } if (null!== $server) {$dup->server = new Serverbag ($server); $dup->headers = new Headerbag ($dup->server->getheaders ()); } $dup->languages = null; $dup->charsets = null; $dup->encodings = null; $dup->acceptablecoNtenttypes = null; $dup->pathinfo = null; $dup->requesturi = null; $dup->baseurl = null; $dup->basepath = null; $dup->method = null; $dup->format = null; if (! $dup->get (' _format ') && $this->get (' _format ') {$dup->attributes->set (' _format '), $thi S->get (' _format ')); } if (! $dup->getrequestformat (null)) {$dup->setrequestformat ($this->getrequestformat (null)); } return $dup; }
After the
Request object is created, in the Laravel application we can easily apply the capabilities it provides, and if you don't know if it implements the function you want, simply go to illuminate\http\ Request
in the source file to see it, all methods are listed in the source file, such as:
/** * Get The full URL for the request. * Gets the requested URL (contains host, not including query string) * * @return string */public function FullUrl () { $query = $this->getquerystring ( ); $question = $this->getbaseurl (). $this->getpathinfo () = = '/'? '/?' : '?'; Return $query? $this->url (). $question. $query: $this->url (); /** * Get The full URL for the request with the added query string parameters. * Gets the full URL that includes the query string * * @param array $query * @return string */public function fullurlwithquery (array $quer Y) { $question = $this->getbaseurl (). $this->getpathinfo () = = '/'? '/?' : '?'; Return count ($this->query ()) > 0 $this->url (). $question. Http_build_query (Array_merge, $this Query (), $query)) : $this->fullurl (). $question. Http_build_query ($query);}
The station where request passes
After the request object is created, the HTTP kernel of the laravel is then executed: loading the service provider to boot the Laravel app, launch the app, let the request pass through the middleware, find the route that the request corresponds to by router match, Executes the matching route, and the request is routed to the middleware to reach the controller method.
Summarize
As the request finally arrives at the corresponding controller method, its mission is basically completed, and in the controller method the input parameters are obtained from the request and then a business logic is executed to obtain the result, and the result is converted into the response response object returned to the requesting client.
This article mainly combs the request object in Laravel, mainly to let you know how to find out what the request of the Laravel has provided for us to use to prevent us from re-building the wheel in the business code to implement the method that the request has already provided.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!