Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Request Class Reference
Inheritance diagram for Request:
Request Http RequestInterface Request RequestContentInterface RequestSafetyInterface HttpRequestInterface RequestInterface PlainTextRequestInterface

Public Member Functions

 __construct (\Magento\Framework\Stdlib\Cookie\CookieReaderInterface $cookieReader, \Magento\Framework\Stdlib\StringUtils $converter, \Magento\Framework\App\AreaList $areaList, \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\Webapi\Rest\Request\DeserializerFactory $deserializerFactory, $uri=null)
 
 getAcceptTypes ()
 
 getBodyParams ()
 
 getContentType ()
 
 getHttpMethod ()
 
 getRequestData ()
 
- Public Member Functions inherited from Request
 __construct (CookieReaderInterface $cookieReader, StringUtils $converter, AreaList $areaList, ScopeInterface $configScope, $uri=null)
 
 getHeader ($header, $default=false)
 
 getRequestedServices ($default=null)
 
- Public Member Functions inherited from Http
 __construct (CookieReaderInterface $cookieReader, StringUtils $converter, ConfigInterface $routeConfig, PathInfoProcessorInterface $pathInfoProcessor, ObjectManagerInterface $objectManager, $uri=null, $directFrontNames=[], PathInfo $pathInfoService=null)
 
 getOriginalPathInfo ()
 
 getPathInfo ()
 
 setPathInfo ($pathInfo=null)
 
 isDirectAccessFrontendName ($code)
 
 getBasePath ()
 
 getFrontName ()
 
 setRouteName ($route)
 
 getRouteName ()
 
 setControllerModule ($module)
 
 getControllerModule ()
 
 initForward ()
 
 getBeforeForwardInfo ($name=null)
 
 isAjax ()
 
 getDistroBaseUrl ()
 
 getFullActionName ($delimiter='_')
 
 __sleep ()
 
 isSafeMethod ()
 
- Public Member Functions inherited from Request
 __construct (CookieReaderInterface $cookieReader, StringUtils $converter, $uri=null)
 
 getModuleName ()
 
 setModuleName ($value)
 
 getControllerName ()
 
 setControllerName ($value)
 
 getActionName ()
 
 setActionName ($value)
 
 getPathInfo ()
 
 setPathInfo ($pathInfo=null)
 
 getRequestString ()
 
 getAlias ($name)
 
 setAlias ($name, $target)
 
 getParam ($key, $default=null)
 
 setParam ($key, $value)
 
 getParams ()
 
 setParams (array $array)
 
 clearParams ()
 
 getScheme ()
 
 setDispatched ($flag=true)
 
 isDispatched ()
 
 isSecure ()
 
 getCookie ($name=null, $default=null)
 
 getServerValue ($name=null, $default=null)
 
 getQueryValue ($name=null, $default=null)
 
 setQueryValue ($name, $value=null)
 
 getPostValue ($name=null, $default=null)
 
 setPostValue ($name, $value=null)
 
 __get ($key)
 
 get ($key)
 
 __isset ($key)
 
 has ($key)
 
 getHeader ($name, $default=false)
 
 getHttpHost ($trimPort=true)
 
 getClientIp ($checkProxy=true)
 
 getUserParams ()
 
 getUserParam ($key, $default=null)
 
 setRequestUri ($requestUri=null)
 
 getBaseUrl ()
 
 isForwarded ()
 
 setForwarded ($forwarded)
 
- Public Member Functions inherited from RequestInterface
 getModuleName ()
 
 setModuleName ($name)
 
 getActionName ()
 
 setActionName ($name)
 
 getParam ($key, $defaultValue=null)
 
 setParams (array $params)
 
 getParams ()
 
 getCookie ($name, $default)
 
 isSecure ()
 
- Public Member Functions inherited from PlainTextRequestInterface
 getContent ()
 
- Public Member Functions inherited from HttpRequestInterface
 isPost ()
 
 isGet ()
 
 isPatch ()
 
 isDelete ()
 
 isPut ()
 

Data Fields

const HTTP_METHOD_GET = 'GET'
 
const HTTP_METHOD_DELETE = 'DELETE'
 
const HTTP_METHOD_PUT = 'PUT'
 
const HTTP_METHOD_POST = 'POST'
 
const REQUEST_CHARSET = 'utf-8'
 
const DEFAULT_ACCEPT = '*/*'
 
- Data Fields inherited from Request
const REQUEST_PARAM_SERVICES = 'services'
 
const ALL_SERVICES = 'all'
 
- Data Fields inherited from Http
const DEFAULT_HTTP_PORT = 80
 
const DEFAULT_HTTPS_PORT = 443
 
const XML_PATH_OFFLOADER_HEADER = 'web/secure/offloader_header'
 
- Data Fields inherited from Request
const SCHEME_HTTP = 'http'
 
const SCHEME_HTTPS = 'https'
 
const XML_PATH_OFFLOADER_HEADER = 'web/secure/offloader_header'
 

Protected Member Functions

 _getDeserializer ()
 
 overrideRequestBodyIdWithPathParam ($urlPathParams)
 
 substituteParameters (&$requestData, $key, $value)
 
- Protected Member Functions inherited from Request
 _convertRequestParamToServiceArray ($param)
 
- Protected Member Functions inherited from Request
 immediateRequestSecure ()
 
 initialRequestSecure ($offLoaderHeader)
 

Protected Attributes

 $_serviceName
 
 $_serviceType
 
 $_deserializer
 
 $_bodyParams
 
 $_deserializerFactory
 
- Protected Attributes inherited from Http
 $route
 
 $pathInfo = ''
 
 $originalPathInfo = ''
 
 $directFrontNames
 
 $controllerModule
 
 $beforeForwardInfo = []
 
 $routeConfig
 
 $pathInfoProcessor
 
 $objectManager
 
 $isSafeMethod = null
 
 $safeRequestTypes = ['GET', 'HEAD', 'TRACE', 'OPTIONS']
 
- Protected Attributes inherited from Request
 $module
 
 $controller
 
 $action
 
 $pathInfo = ''
 
 $requestString = ''
 
 $params = []
 
 $aliases = []
 
 $dispatched = false
 
 $forwarded
 
 $cookieReader
 
 $converter
 
 $appConfig
 
 $sslOffloadHeader
 

Additional Inherited Members

- Static Public Member Functions inherited from Http
static getDistroBaseUrlPath ($server)
 
static getUrlNoScript ($url)
 

Detailed Description

Definition at line 14 of file Request.php.

Constructor & Destructor Documentation

◆ __construct()

Initialize dependencies

Parameters
\Magento\Framework\Stdlib\Cookie\CookieReaderInterface$cookieReader
\Magento\Framework\Stdlib\StringUtils$converter
\Magento\Framework\App\AreaList$areaList
\Magento\Framework\Config\ScopeInterface$configScope
\Magento\Framework\Webapi\Rest\Request\DeserializerFactory$deserializerFactory
null | string$uri

Definition at line 67 of file Request.php.

74  {
75  parent::__construct($cookieReader, $converter, $areaList, $configScope, $uri);
76  $this->_deserializerFactory = $deserializerFactory;
77  }

Member Function Documentation

◆ _getDeserializer()

_getDeserializer ( )
protected

Get request deserializer.

Returns
\Magento\Framework\Webapi\Rest\Request\DeserializerInterface

Definition at line 84 of file Request.php.

85  {
86  if (null === $this->_deserializer) {
87  $this->_deserializer = $this->_deserializerFactory->get($this->getContentType());
88  }
89  return $this->_deserializer;
90  }

◆ getAcceptTypes()

getAcceptTypes ( )

Retrieve accept types understandable by requester in a form of array sorted by quality in descending order.

Returns
string[]

Definition at line 97 of file Request.php.

98  {
99  $qualityToTypes = [];
100  $orderedTypes = [];
101 
102  foreach (preg_split('/,\s*/', $this->getHeader('Accept')) as $definition) {
103  $typeWithQ = explode(';', $definition);
104  $mimeType = trim(array_shift($typeWithQ));
105 
106  // check MIME type validity
107  if (!preg_match('~^([0-9a-z*+\-]+)(?:/([0-9a-z*+\-\.]+))?$~i', $mimeType)) {
108  continue;
109  }
110  $quality = '1.0';
111  // default value for quality
112 
113  if ($typeWithQ) {
114  $qAndValue = explode('=', $typeWithQ[0]);
115 
116  if (2 == count($qAndValue)) {
117  $quality = $qAndValue[1];
118  }
119  }
120  $qualityToTypes[$quality][$mimeType] = true;
121  }
122  krsort($qualityToTypes);
123 
124  foreach ($qualityToTypes as $typeList) {
125  $orderedTypes += $typeList;
126  }
127  return empty($orderedTypes) ? [self::DEFAULT_ACCEPT] : array_keys($orderedTypes);
128  }
getHeader($header, $default=false)
Definition: Request.php:62

◆ getBodyParams()

getBodyParams ( )

Fetch data from HTTP Request body.

Returns
array

Definition at line 135 of file Request.php.

136  {
137  if (null == $this->_bodyParams) {
138  $this->_bodyParams = [];
139  //avoid JSON decoding with empty string
140  if ($this->getContent()) {
141  $this->_bodyParams = (array)$this->_getDeserializer()->deserialize((string)$this->getContent());
142  }
143  }
144  return $this->_bodyParams;
145  }

◆ getContentType()

getContentType ( )

Get Content-Type of request.

Returns
string
Exceptions

Definition at line 153 of file Request.php.

154  {
155  $headerValue = $this->getHeader('Content-Type');
156 
157  if (!$headerValue) {
158  throw new \Magento\Framework\Exception\InputException(new Phrase('Content-Type header is empty.'));
159  }
160  if (!preg_match('~^([a-z\d/\-+.]+)(?:; *charset=(.+))?$~Ui', $headerValue, $matches)) {
161  throw new \Magento\Framework\Exception\InputException(new Phrase('Content-Type header is invalid.'));
162  }
163  // request encoding check if it is specified in header
164  if (isset($matches[2]) && self::REQUEST_CHARSET != strtolower($matches[2])) {
165  throw new \Magento\Framework\Exception\InputException(new Phrase('UTF-8 is the only supported charset.'));
166  }
167 
168  return $matches[1];
169  }
getHeader($header, $default=false)
Definition: Request.php:62

◆ getHttpMethod()

getHttpMethod ( )

Retrieve current HTTP method.

Returns
string
Exceptions

Definition at line 177 of file Request.php.

178  {
179  if (!$this->isGet() && !$this->isPost() && !$this->isPut() && !$this->isDelete()) {
180  throw new \Magento\Framework\Exception\InputException(new Phrase('Request method is invalid.'));
181  }
182  return $this->getMethod();
183  }

◆ getRequestData()

getRequestData ( )

Fetch and return parameter data from the request.

Returns
array

Definition at line 190 of file Request.php.

191  {
192  $requestBodyParams = [];
193  $params = $this->getParams();
194 
195  $httpMethod = $this->getHttpMethod();
196  if ($httpMethod == self::HTTP_METHOD_POST ||
197  $httpMethod == self::HTTP_METHOD_PUT
198  ) {
199  $requestBodyParams = $this->getBodyParams();
200  }
201 
202  return array_merge($requestBodyParams, $params);
203  }

◆ overrideRequestBodyIdWithPathParam()

overrideRequestBodyIdWithPathParam (   $urlPathParams)
protected

Override request body property value with matching url path parameter value

This method assumes that webapi.xml url defines the substitution parameter as camelCase to the actual snake case key described as part of the api contract. example: /:parentId/nestedResource/:entityId. Here :entityId value will be used for overriding 'entity_id' property in the body. Since Webapi framework allows both camelCase and snakeCase, either of them will be substituted for now. If the request body is missing url path parameter as property, it will be added to the body. This method works only requests with scalar properties at top level or properties of single object embedded in the request body. Only the last path parameter value will be substituted from the url in case of multiple parameters.

Parameters
array$urlPathParamsurl path parameters as array
Returns
array
Deprecated:
100.1.0
See also
\Magento\Webapi\Controller\Rest\ParamsOverrider::overrideRequestBodyIdWithPathParam

Definition at line 223 of file Request.php.

224  {
225  $requestBodyParams = $this->getBodyParams();
226  $pathParamValue = end($urlPathParams);
227  // Self apis should not be overridden
228  if ($pathParamValue === 'me') {
229  return $requestBodyParams;
230  }
231  $pathParamKey = key($urlPathParams);
232  // Check if the request data is a top level object of body
233  if (count($requestBodyParams) == 1 && is_array(end($requestBodyParams))) {
234  $requestDataKey = key($requestBodyParams);
235  $this->substituteParameters($requestBodyParams[$requestDataKey], $pathParamKey, $pathParamValue);
236  } else { // Else parameters passed as scalar values in body will be overridden
237  $this->substituteParameters($requestBodyParams, $pathParamKey, $pathParamValue);
238  }
239 
240  return $requestBodyParams;
241  }
substituteParameters(&$requestData, $key, $value)
Definition: Request.php:253

◆ substituteParameters()

substituteParameters ( $requestData,
  $key,
  $value 
)
protected

Check presence for both camelCase and snake_case keys in array and substitute if either is present

Parameters
array$requestData
string$key
string$value
Returns
void
Deprecated:
100.1.0
See also
\Magento\Webapi\Controller\Rest\ParamsOverrider::substituteParameters

Definition at line 253 of file Request.php.

254  {
257 
258  if (isset($requestData[$camelCaseKey])) {
259  $requestData[$camelCaseKey] = $value;
260  } else {
261  $requestData[$snakeCaseKey] = $value;
262  }
263  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $_bodyParams

$_bodyParams
protected

Definition at line 50 of file Request.php.

◆ $_deserializer

$_deserializer
protected

Definition at line 45 of file Request.php.

◆ $_deserializerFactory

$_deserializerFactory
protected

Definition at line 55 of file Request.php.

◆ $_serviceName

$_serviceName
protected

Definition at line 35 of file Request.php.

◆ $_serviceType

$_serviceType
protected

Definition at line 40 of file Request.php.

◆ DEFAULT_ACCEPT

const DEFAULT_ACCEPT = '*/*'

Definition at line 30 of file Request.php.

◆ HTTP_METHOD_DELETE

const HTTP_METHOD_DELETE = 'DELETE'

Definition at line 20 of file Request.php.

◆ HTTP_METHOD_GET

const HTTP_METHOD_GET = 'GET'

#+ HTTP methods supported by REST.

Definition at line 19 of file Request.php.

◆ HTTP_METHOD_POST

const HTTP_METHOD_POST = 'POST'

Definition at line 22 of file Request.php.

◆ HTTP_METHOD_PUT

const HTTP_METHOD_PUT = 'PUT'

Definition at line 21 of file Request.php.

◆ REQUEST_CHARSET

const REQUEST_CHARSET = 'utf-8'

#- Character set which must be used in request.

Definition at line 28 of file Request.php.


The documentation for this class was generated from the following file: