Ver Mensaje Individual
  #10 (permalink)  
Antiguo 21/09/2015, 15:41
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años
Puntos: 320
Respuesta: Como estructurar mi Proyecto Web

Es algo asi:
Código PHP:
Ver original
  1. class Route
  2.  {
  3.     /*/
  4.      * I18n
  5.     /*/
  6.     const ERROR_LOADF = "No se pudo cargar la url.";
  7.     const ERROR_CLASS = "No se pudo encontrar el recurso solicitado.";
  8.     const ERROR_CALLM = "El recurso no reconoce la accion solicitada.";
  9.  
  10.     public static function load(&$class, $sys, &$method = null)
  11.     {
  12.         $class = str_replace("_", "/", $class);
  13.        
  14.         if(!class_exists($class))
  15.         {
  16.             $file = "applications/$sys/$class.php";
  17.             if(!file_exists($file))
  18.                 throw new \Exception(self::ERROR_LOADF, 601);
  19.             require_once($file);
  20.            
  21.             $pos = strrpos($class, "/") + 1;
  22.             $class[$pos] = ucfirst($class[$pos]);
  23.             $class = "\\".str_replace("/",  "\\", $class);
  24.            
  25.             if(!class_exists($class))
  26.                 throw new \Exception(self::ERROR_CLASS, 602);
  27.         }
  28.  
  29.         if(!is_null($method) && ($method[0] === "_" || !is_callable([$class, $method])))
  30.             throw new \Exception(self::ERROR_CALLM, 603);
  31.  
  32.         unset($file, $pos);
  33.     }
  34.  
  35.     public static function resolve($sys, $route = "") {
  36.         try {
  37.             require_once("core/dbal.php");
  38.             if($_SERVER["REQUEST_METHOD"] == "POST")
  39.             {
  40.                 list($class, $method) = array_filter(explode("-", strtolower(trim($route, "/ ")), 2)) + ["index", "index"];
  41.                
  42.                 self::load($class, $sys, $method);
  43.                                
  44.                 $instance = new $class();
  45.                 $params = array_merge($_GET, $_POST);
  46.                
  47.                 unset($class, $_GET, $_POST, $_FILES);
  48.  
  49.                 echo json_encode($instance->$method($params));
  50.             }
  51.         }
  52.         catch(\Exception $e)
  53.         {
  54.             require_once("applications/$sys/error.php");
  55.             $instance = new \Error();
  56.             $method = strtolower($_SERVER["REQUEST_METHOD"])."_500";
  57.             echo $instance->$method($route, $e, $sys);
  58.         }
  59.     }
  60. }
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios