Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
router.php
Go to the documentation of this file.
1 <?php
20 define('DEBUG_ROUTER', false);
21 
22 $debug = function ($val) {
23  if (!DEBUG_ROUTER) {
24  return;
25  }
26 
27  if (is_array($val)) {
28  $val = json_encode($val);
29  }
30 
31  error_log('debug: '.$val);
32 };
33 
40 if (php_sapi_name() === 'cli-server') {
41  $debug("URI: {$_SERVER["REQUEST_URI"]}");
42  if (preg_match('/^\/(index|get|static)\.php(\/)?/', $_SERVER["REQUEST_URI"])) {
43  return false; // serve the requested resource as-is.
44  }
45 
46  $path = pathinfo($_SERVER["SCRIPT_FILENAME"]);
47  $url = pathinfo(substr($_SERVER["REQUEST_URI"], 1));
48  $route = parse_url(substr($_SERVER["REQUEST_URI"], 1))["path"];
49  $pathinfo = pathinfo($route);
50  $ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';
51 
52  if ($path["basename"] == 'favicon.ico') {
53  return false;
54  }
55 
56  $debug("route: $route");
57 
58  if (strpos($route, 'pub/errors/default/') === 0) {
59  $route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1);
60  }
61 
62  $magentoPackagePubDir = __DIR__."/../pub";
63 
64  if (strpos($route, 'media/') === 0 ||
65  strpos($route, 'opt/') === 0 ||
66  strpos($route, 'static/') === 0 ||
67  strpos($route, 'errors/default/css/') === 0 ||
68  strpos($route, 'errors/default/images/') === 0
69  ) {
70  $origFile = $magentoPackagePubDir.'/'.$route;
71 
72  if (strpos($route, 'static/version') === 0) {
73  $route = preg_replace('#static/(version\d+/)?#', 'static/', $route, 1);
74  }
75  $file = $magentoPackagePubDir.'/'.$route;
76 
77  $debug("file: $file");
78 
79  if (file_exists($origFile) || file_exists($file)) {
80  if (file_exists($origFile)) {
81  $file = $origFile;
82  }
83 
84  $debug('file exists');
85  $mimeTypes = [
86  'css' => 'text/css',
87  'js' => 'application/javascript',
88  'jpg' => 'image/jpg',
89  'png' => 'image/png',
90  'gif' => 'image/gif',
91  'svg' => 'image/svg+xml',
92  'map' => 'application/json',
93  'woff' => 'application/x-woff',
94  'woff2' => 'application/font-woff2',
95  'html' => 'text/html',
96  ];
97 
98  if (isset($mimeTypes[$ext])) {
99  header("Content-Type: $mimeTypes[$ext]");
100  }
101  readfile($file);
102  return;
103  } else {
104  $debug('file does not exist');
105  if (strpos($route, 'static/') === 0) {
106  $route = preg_replace('#static/#', '', $route, 1);
107  $_GET['resource'] = $route;
108  $debug("static: $route");
109  include($magentoPackagePubDir.'/static.php');
110  exit;
111  } elseif (strpos($route, 'media/') === 0) {
112  $debug("media: $route");
113  include($magentoPackagePubDir.'/get.php');
114  exit;
115  }
116  }
117  } else {
118  $debug("thunk to index in $route");
119  include($magentoPackagePubDir.'/index.php');
120  }
121 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
const DEBUG_ROUTER
Definition: router.php:20
exit
Definition: redirect.phtml:12
$debug
Definition: router.php:22