Advertisement
villers

Untitled

Nov 25th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MyFramework;
  4.  
  5. class Core
  6. {
  7.     static protected $_routing = [];
  8.     static private $_render;
  9.  
  10.     private function routing()
  11.     {
  12.         self::$_render = [
  13.             'controller' => 'default',
  14.             'action' => 'default'
  15.         ];
  16.         self::$_routing = self::$_render;
  17.         $url = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
  18.         if($url[0] == trim(BASE_URI, '/')) {
  19.             array_shift($url);
  20.         }
  21.         if(!isset($url[0])) {
  22.             return;
  23.         }
  24.         if(file_exists('controllers'.DS.ucfirst($url[0]).'Controller.class.php')) {
  25.             self::$_routing['controller'] = array_shift($url);
  26.             if(isset($url[0])) {
  27.                 self::$_routing['action'] = array_shift($url);
  28.             }
  29.         } else {
  30.             echo "Impossible de trouver la classe" . PHP_EOL;
  31.         }
  32.     }
  33.  
  34.     public function run()
  35.     {
  36.         $this->routing();
  37.         $c = __NAMESPACE__ . '\\' . ucfirst(self::$_routing['controller']) . 'Controller';
  38.         $o = new $c();
  39.         if (method_exists($o, $a = self::$_routing['action'] . 'Action')) {
  40.             self::$_render = $o->$a();
  41.         }
  42.         else {
  43.             self::$_render = "Impossible de trouver la methode" . PHP_EOL;
  44.         }
  45.         echo self::$_render;
  46.     }
  47.  
  48.     protected function render($params = [])
  49.     {
  50.         $f = implode(DS, [
  51.             dirname(__DIR__),
  52.             'views',
  53.             self::$_routing['controller'],
  54.             self::$_routing['action'].'.html'
  55.         ]);
  56.         if (file_exists($f)) {
  57.             $c = file_get_contents($f);
  58.             foreach ($params as $k => $v) {
  59.                 $c = preg_replace("/\{\{\s*$k\s*\}\}/", $v, $c);
  60.             }
  61.             self::$_render = $c;
  62.         }
  63.         else {
  64.             self::$_render = "Impossible de trouver la vue" . PHP_EOL;
  65.         }
  66.         echo self::$_render; // j'ai ajouté cette ligne
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement