Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Http.php
Go to the documentation of this file.
1 <?php
7 
16 
21 {
25  const DEFAULT_HTTP_PORT = 80;
26  const DEFAULT_HTTPS_PORT = 443;
29  // Configuration path
30  const XML_PATH_OFFLOADER_HEADER = 'web/secure/offloader_header';
31 
35  protected $route;
36 
42  protected $pathInfo = '';
43 
49  protected $originalPathInfo = '';
50 
54  protected $directFrontNames;
55 
59  protected $controllerModule;
60 
66  protected $beforeForwardInfo = [];
67 
71  protected $routeConfig;
72 
76  protected $pathInfoProcessor;
77 
81  protected $objectManager;
82 
86  protected $isSafeMethod = null;
87 
91  protected $safeRequestTypes = ['GET', 'HEAD', 'TRACE', 'OPTIONS'];
92 
96  private $distroBaseUrl;
97 
101  private $pathInfoService;
102 
113  public function __construct(
119  $uri = null,
120  $directFrontNames = [],
121  PathInfo $pathInfoService = null
122  ) {
123  parent::__construct($cookieReader, $converter, $uri);
124  $this->routeConfig = $routeConfig;
125  $this->pathInfoProcessor = $pathInfoProcessor;
126  $this->objectManager = $objectManager;
127  $this->directFrontNames = $directFrontNames;
128  $this->pathInfoService = $pathInfoService ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
129  PathInfo::class
130  );
131  }
132 
140  public function getOriginalPathInfo()
141  {
142  if (empty($this->originalPathInfo)) {
143  $originalPathInfoFromRequest = $this->pathInfoService->getPathInfo(
144  $this->getRequestUri(),
145  $this->getBaseUrl()
146  );
147  $this->originalPathInfo = (string)$this->pathInfoProcessor->process($this, $originalPathInfoFromRequest);
148  $this->requestString = $this->originalPathInfo
149  . $this->pathInfoService->getQueryString($this->getRequestUri());
150  }
152  }
153 
159  public function getPathInfo()
160  {
161  if (empty($this->pathInfo)) {
162  $this->pathInfo = $this->getOriginalPathInfo();
163  }
164  return $this->pathInfo;
165  }
166 
175  public function setPathInfo($pathInfo = null)
176  {
177  $this->pathInfo = (string)$pathInfo;
178  return $this;
179  }
180 
190  {
191  return isset($this->directFrontNames[$code]);
192  }
193 
199  public function getBasePath()
200  {
201  $path = parent::getBasePath();
202  if (empty($path)) {
203  $path = '/';
204  } else {
205  $path = str_replace('\\', '/', $path);
206  }
207  return $path;
208  }
209 
215  public function getFrontName()
216  {
217  $pathParts = explode('/', trim($this->getPathInfo(), '/'));
218  return reset($pathParts);
219  }
220 
227  public function setRouteName($route)
228  {
229  $this->route = $route;
230  $module = $this->routeConfig->getRouteFrontName($route);
231  if ($module) {
232  $this->setModuleName($module);
233  }
234  return $this;
235  }
236 
242  public function getRouteName()
243  {
244  return $this->route;
245  }
246 
253  public function setControllerModule($module)
254  {
255  $this->controllerModule = $module;
256  return $this;
257  }
258 
264  public function getControllerModule()
265  {
267  }
268 
274  public function initForward()
275  {
276  if (empty($this->beforeForwardInfo)) {
277  $this->beforeForwardInfo = [
278  'params' => $this->getParams(),
279  'action_name' => $this->getActionName(),
280  'controller_name' => $this->getControllerName(),
281  'module_name' => $this->getModuleName(),
282  'route_name' => $this->getRouteName(),
283  ];
284  }
285  return $this;
286  }
287 
296  public function getBeforeForwardInfo($name = null)
297  {
298  if ($name === null) {
300  } elseif (isset($this->beforeForwardInfo[$name])) {
301  return $this->beforeForwardInfo[$name];
302  }
303  return null;
304  }
305 
311  public function isAjax()
312  {
313  if ($this->isXmlHttpRequest()) {
314  return true;
315  }
316  if ($this->getParam('ajax') || $this->getParam('isAjax')) {
317  return true;
318  }
319  return false;
320  }
321 
329  public function getDistroBaseUrl()
330  {
331  if ($this->distroBaseUrl) {
332  return $this->distroBaseUrl;
333  }
334  $headerHttpHost = $this->getServer('HTTP_HOST');
335  $headerHttpHost = $this->converter->cleanString($headerHttpHost);
336  $headerScriptName = $this->getServer('SCRIPT_NAME');
337 
338  if (isset($headerScriptName) && isset($headerHttpHost)) {
339  if ($secure = $this->isSecure()) {
340  $scheme = 'https://';
341  } else {
342  $scheme = 'http://';
343  }
344 
345  $hostArr = explode(':', $headerHttpHost);
346  $host = $hostArr[0];
347  $port = isset($hostArr[1])
348  && (!$secure && $hostArr[1] != 80 || $secure && $hostArr[1] != 443) ? ':' . $hostArr[1] : '';
349  $path = $this->getBasePath();
350 
351  return $this->distroBaseUrl = $scheme . $host . $port . rtrim($path, '/') . '/';
352  }
353  return 'http://localhost/';
354  }
355 
362  public static function getDistroBaseUrlPath($server)
363  {
364  $result = '';
365  if (isset($server['SCRIPT_NAME'])) {
366  $envPath = str_replace('\\', '/', dirname(str_replace('\\', '/', $server['SCRIPT_NAME'])));
367  if ($envPath != '.' && $envPath != '/') {
368  $result = $envPath;
369  }
370  }
371  if (!preg_match('/\/$/', $result)) {
372  $result .= '/';
373  }
374  return $result;
375  }
376 
383  public static function getUrlNoScript($url)
384  {
385  if (!isset($_SERVER['SCRIPT_NAME'])) {
386  return $url;
387  }
388 
389  if (($pos = strripos($url, basename($_SERVER['SCRIPT_NAME']))) !== false) {
390  $url = substr($url, 0, $pos);
391  }
392 
393  return $url;
394  }
395 
402  public function getFullActionName($delimiter = '_')
403  {
404  return $this->getRouteName() .
405  $delimiter .
406  $this->getControllerName() .
407  $delimiter .
408  $this->getActionName();
409  }
410 
416  public function __sleep()
417  {
418  return [];
419  }
420 
424  public function isSafeMethod()
425  {
426  if ($this->isSafeMethod === null) {
427  if (isset($_SERVER['REQUEST_METHOD']) && (in_array($_SERVER['REQUEST_METHOD'], $this->safeRequestTypes))) {
428  $this->isSafeMethod = true;
429  } else {
430  $this->isSafeMethod = false;
431  }
432  }
433  return $this->isSafeMethod;
434  }
435 }
static getDistroBaseUrlPath($server)
Definition: Http.php:362
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getFullActionName($delimiter='_')
Definition: Http.php:402
$pos
Definition: list.phtml:42
__construct(CookieReaderInterface $cookieReader, StringUtils $converter, ConfigInterface $routeConfig, PathInfoProcessorInterface $pathInfoProcessor, ObjectManagerInterface $objectManager, $uri=null, $directFrontNames=[], PathInfo $pathInfoService=null)
Definition: Http.php:113
setPathInfo($pathInfo=null)
Definition: Http.php:175
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14