Class ServerRequestFactory
Factory for making ServerRequest instances.
This subclass adds in CakePHP specific behavior to populate the basePath and webroot attributes. Furthermore the Uri's path is corrected to only contain the 'virtual' path for the request.
Method Summary
-
createServerRequest() public
Create a new server request.
-
createUri() public static
Create a new Uri instance from the provided server data.
-
fromGlobals() public static
Create a request from the supplied superglobal values.
-
getBase() protected static
Calculate the base directory and webroot directory.
-
marshalBodyAndRequestMethod() protected static
Sets the REQUEST_METHOD environment variable based on the simulated _method HTTP override value. The 'ORIGINAL_REQUEST_METHOD' is also preserved, if you want the read the non-simulated HTTP method the client used.
-
marshalFiles() protected static
Process uploaded files and move things onto the parsed body.
-
marshalUriFromSapi() protected static
Build a UriInterface object.
-
updatePath() protected static
Updates the request URI to remove the base directory.
Method Detail
createServerRequest() ¶ public
createServerRequest(string $method, UriInterface|string $uri, array $serverParams = []): Psr\Http\Message\ServerRequestInterface
Create a new server request.
Note that server-params are taken precisely as given - no parsing/processing of the given values is performed, and, in particular, no attempt is made to determine the HTTP method or URI, which must be provided explicitly.
Parameters
-
string
$method The HTTP method associated with the request.
-
UriInterface|string
$uri The URI associated with the request. If the value is a string, the factory MUST create a UriInterface instance based on it.
-
array
$serverParams optional Array of SAPI parameters with which to seed the generated request instance.
Returns
Psr\Http\Message\ServerRequestInterface
createUri() ¶ public static
createUri(array $server = []): Psr\Http\Message\UriInterface
Create a new Uri instance from the provided server data.
Parameters
-
array
$server optional Array of server data to build the Uri from. $_SERVER will be added into the $server parameter.
Returns
Psr\Http\Message\UriInterface
fromGlobals() ¶ public static
fromGlobals(array $server = null, array $query = null, array $parsedBody = null, array $cookies = null, array $files = null): Cake\Http\ServerRequest
Create a request from the supplied superglobal values.
If any argument is not supplied, the corresponding superglobal value will be used.
The ServerRequest created is then passed to the fromServer() method in order to marshal the request URI and headers.
Parameters
-
array
$server optional $_SERVER superglobal
-
array
$query optional $_GET superglobal
-
array
$parsedBody optional $_POST superglobal
-
array
$cookies optional $_COOKIE superglobal
-
array
$files optional $_FILES superglobal
Returns
Cake\Http\ServerRequest
Throws
InvalidArgumentException
for invalid file values
See Also
getBase() ¶ protected static
getBase(Psr\Http\Message\UriInterface $uri, array $server): array
Calculate the base directory and webroot directory.
Parameters
-
Psr\Http\Message\UriInterface
$uri The Uri instance.
-
array
$server The SERVER data to use.
Returns
array
marshalBodyAndRequestMethod() ¶ protected static
marshalBodyAndRequestMethod(array $parsedBody, Cake\Http\ServerRequest $request): Cake\Http\ServerRequest
Sets the REQUEST_METHOD environment variable based on the simulated _method HTTP override value. The 'ORIGINAL_REQUEST_METHOD' is also preserved, if you want the read the non-simulated HTTP method the client used.
Request body of content type "application/x-www-form-urlencoded" is parsed into array for PUT/PATCH/DELETE requests.
Parameters
-
array
$parsedBody Parsed body.
-
Cake\Http\ServerRequest
$request Request instance.
Returns
Cake\Http\ServerRequest
marshalFiles() ¶ protected static
marshalFiles(array $files, Cake\Http\ServerRequest $request): Cake\Http\ServerRequest
Process uploaded files and move things onto the parsed body.
Parameters
-
array
$files Files array for normalization and merging in parsed body.
-
Cake\Http\ServerRequest
$request Request instance.
Returns
Cake\Http\ServerRequest
marshalUriFromSapi() ¶ protected static
marshalUriFromSapi(array $server, array $headers): Psr\Http\Message\UriInterface
Build a UriInterface object.
Add in some CakePHP specific logic/properties that help preserve backwards compatibility.
Parameters
-
array
$server The server parameters.
-
array
$headers The normalized headers
Returns
Psr\Http\Message\UriInterface
updatePath() ¶ protected static
updatePath(string $base, Psr\Http\Message\UriInterface $uri): Psr\Http\Message\UriInterface
Updates the request URI to remove the base directory.
Parameters
-
string
$base The base path to remove.
-
Psr\Http\Message\UriInterface
$uri The uri to update.
Returns
Psr\Http\Message\UriInterface