Created
October 19, 2014 18:49
-
-
Save Flyingmana/f51110d775051fee41c2 to your computer and use it in GitHub Desktop.
Magento2 router.php for php built-in Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$debug = function($val){ | |
return; | |
echo $val.PHP_EOL.'<br/>'.PHP_EOL; | |
}; | |
/** | |
* Caution, this is very experimental stuff | |
* no garant for working result | |
* has tons of potencial big security holes | |
*/ | |
if (php_sapi_name() == 'cli-server') { | |
/* route static assets and return false */ | |
$path = pathinfo($_SERVER["SCRIPT_FILENAME"]); | |
/* | |
array(4) { | |
["dirname"]=> string(25) "/work/cotya/mage_2/public" | |
["basename"]=> string(9) "index.php" | |
["extension"]=> string(3) "php" | |
["filename"]=> string(5) "index" | |
} | |
*/ | |
$url = pathinfo(substr($_SERVER["REQUEST_URI"], 1)); | |
$route = parse_url(substr($_SERVER["REQUEST_URI"], 1))["path"]; | |
$debug($path['extension']); | |
$outputFile = function($filePath) use ($url){ | |
$mapping = [ | |
'ico' => 'image/x-icon', | |
'css' => 'text/css', | |
'js' => 'text/javascript', | |
]; | |
if (isset($mapping[$url['extension']])) { | |
header("Content-Type: ".$mapping[$url['extension']]); | |
} | |
readfile($filePath); | |
exit; | |
}; | |
if( | |
$path["basename"] == 'favicon.ico' | |
){ | |
$outputFile($_SERVER["SCRIPT_FILENAME"]); | |
} | |
$debug($route); | |
$debug(strpos($route, 'errors/default/css/')); | |
$magentoPackageDir = __DIR__.'/../vendor/magento/project-community-edition'; | |
if(strpos($route, 'pub/errors/default/') === 0){ | |
$route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1); | |
} | |
$debug($route); | |
if( | |
strpos($route, 'media/') === 0 || | |
strpos($route, 'opt/') === 0 || | |
strpos($route, 'static/') === 0 || | |
strpos($route, 'errors/default/css/') === 0 || | |
strpos($route, 'errors/default/images/') === 0 || | |
false | |
){ | |
$magentoPackagePubDir = $magentoPackageDir.'/pub'; | |
$file = $magentoPackagePubDir.'/'.$route; | |
$debug($file); | |
if(file_exists($file)){ | |
$outputFile($file); | |
}else{ | |
if(strpos($route, 'static/') === 0){ | |
$route = preg_replace('#static/#', '', $route, 1); | |
$_GET['resource'] = $route; | |
include($magentoPackagePubDir.'/static.php'); | |
exit; | |
}elseif(strpos($route, 'media/') === 0){ | |
include($magentoPackagePubDir.'/get.php'); | |
exit; | |
} | |
} | |
} | |
if(strpos($route, 'pub/') === 0){ | |
$magentoPackageSetupDir = $magentoPackageDir.'/setup'; | |
$file = $magentoPackageSetupDir.'/'.$route; | |
$debug($file); | |
if(file_exists($file)){ | |
$outputFile($file); | |
} | |
} | |
if(strpos($route, 'setup/') === 0){ | |
$magentoPackageSetupDir = $magentoPackageDir.'/setup'; | |
include($magentoPackageSetupDir.'/index.php'); | |
exit; | |
} | |
} | |
/* go on with normal index.php operations */ | |
include(__DIR__.'/index.php'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment