示例#1
0
 /**
  * Function to parse route request
  * 
  * @static
  * @access public
  * @return void
  */
 public static function Parse()
 {
     $oThis = self::CreateInstanceIfNotExists();
     $sRoot = str_replace(array("index.php", " "), array("", "%20"), $_SERVER["SCRIPT_NAME"]);
     //Bugfix
     $sUri = $sRoot != "/" ? str_replace($sRoot, "", $_SERVER["REQUEST_URI"]) : substr($_SERVER["REQUEST_URI"], 1, strlen($_SERVER["REQUEST_URI"]) - 1);
     $aParsedRoute = explode("/", $sUri);
     $mID = array_key_exists(1, $aParsedRoute) ? $aParsedRoute[1] : null;
     $sMethod = $oThis->Restful();
     $oThis->RestParams();
     if (!$oThis->bOverloadFrontend) {
         $bResult = Bootstrap::AutoLoad(!empty($aParsedRoute[0]) && $aParsedRoute[0] != "/" ? strtolower(str_replace("@", "", $aParsedRoute[0])) : "main");
         if (!$bResult) {
             Bootstrap::AutoLoad("main");
         }
     }
     $sUri = preg_replace("/\\?.*\$/", "", $sUri);
     //Removendo path
     $sUri = preg_replace("/\\#.*\$/", "", $sUri);
     //Removendo path
     $bCall = false;
     $aIdent = array("int" => "\\d+", "str" => "\\w+", "flt" => "[\\d.]+");
     foreach ($oThis->aRoutes as $sRoute => $fFunc) {
         if (preg_match_all('/{(?P<field>\\w+)(:((?P<type>\\w{3,3})|regex\\((?P<regex>.*)\\)))?(:(?P<notnull>notnull))?}/i', $sRoute, $aMatches)) {
             if (preg_match('/(?P<pre>.*\\w)\\/\\{/i', $sRoute, $aMatch)) {
                 $sOk = str_replace('/', '\\/', $aMatch['pre']);
                 $sRoute = str_replace($aMatch['pre'], $sOk, $sRoute);
             }
             foreach ($aMatches['field'] as $iKey => $sValue) {
                 if ($aMatches['regex'][$iKey]) {
                     if ($aMatches['notnull'][$iKey] == 'notnull') {
                         $sRoute = preg_replace('/\\/{' . $aMatches['field'][$iKey] . ':regex(' . $aMatches['regex'][$iKey] . '):notnull}/i', "\\/(" . $aMatches['regex'][$iKey] . ")", $sRoute);
                     } else {
                         $sRoute = preg_replace('/\\/{' . $aMatches['field'][$iKey] . ':regex(' . $aMatches['regex'][$iKey] . ')}/i', "\\/?(" . $aMatches['regex'][$iKey] . ")?", $sRoute);
                     }
                 } else {
                     if ($aMatches['type'][$iKey]) {
                         if ($aMatches['notnull'][$iKey] == 'notnull') {
                             $sRoute = preg_replace('/\\/{' . $aMatches['field'][$iKey] . ':' . $aMatches['type'][$iKey] . ':notnull}/i', "\\/(" . $aIdent[$aMatches['type'][$iKey]] . ")", $sRoute);
                         } else {
                             $sRoute = preg_replace('/\\/?{' . $aMatches['field'][$iKey] . ':' . $aMatches['type'][$iKey] . '}/i', "\\/?(" . $aIdent[$aMatches['type'][$iKey]] . ")?", $sRoute);
                         }
                     } else {
                         if ($aMatches['notnull'][$iKey] == 'notnull') {
                             $sRoute = preg_replace('/\\/{' . $aMatches['field'][$iKey] . ':notnull}/i', "\\/([^\\/]*)", $sRoute);
                         } else {
                             $sRoute = preg_replace("/\\/?{" . $aMatches['field'][$iKey] . "}/", "\\/?([^\\/]*)?", $sRoute);
                         }
                     }
                 }
             }
         } else {
             $sRoute = str_replace('/', '\\/', $sRoute);
         }
         if (preg_match_all("/^" . $sRoute . "\\/?\$/i", $sMethod . "_" . $sUri, $aMatches)) {
             $aParams = array();
             foreach ($aMatches as $iKey => $aResult) {
                 if ($iKey > 0) {
                     $aParams[] = $aResult[0];
                 }
             }
             $bCall = true;
             Storage::Set("route.request", $sRoute);
             if (is_array($fFunc)) {
                 if (is_array($fFunc[1])) {
                     $aParams = array_merge($fFunc[1], $aParams);
                 } else {
                     $aParams = array_merge(array($fFunc[1]), $aParams);
                 }
                 call_user_func_array($fFunc[0], $aParams);
             } else {
                 call_user_func_array($fFunc, $aParams);
             }
             break;
         }
     }
     if (array_key_exists("__dynamicroute", $oThis->aRoutes) && !$bCall) {
         call_user_func($oThis->aRoutes["__dynamicroute"]);
     } else {
         Output::SendHTTPCode(404);
     }
 }
示例#2
0
<?php

/**
 * Routes of Hello World
 * 
 * @package     MagicPHP Hello World
 * @author      André Ferreira <*****@*****.**>
 * @link        https://github.com/magicphp/magicphp MagicPHP(tm)
 * @license     MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
Routes::SetOverloadFrontend(true);
Routes::Set("", "GET", "App\\Helloworld\\Controllers\\Helloworld::Index");
Routes::SetDynamicRoute(function () {
    Output::SendHTTPCode(404);
});