Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace MyFramework;
- class Core
- {
- static protected $_routing = [];
- static private $_render;
- private function routing()
- {
- self::$_render = [
- 'controller' => 'default',
- 'action' => 'default'
- ];
- self::$_routing = self::$_render;
- $url = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
- if($url[0] == trim(BASE_URI, '/')) {
- array_shift($url);
- }
- if(!isset($url[0])) {
- return;
- }
- if(file_exists('controllers'.DS.ucfirst($url[0]).'Controller.class.php')) {
- self::$_routing['controller'] = array_shift($url);
- if(isset($url[0])) {
- self::$_routing['action'] = array_shift($url);
- }
- } else {
- echo "Impossible de trouver la classe" . PHP_EOL;
- }
- }
- public function run()
- {
- $this->routing();
- $c = __NAMESPACE__ . '\\' . ucfirst(self::$_routing['controller']) . 'Controller';
- $o = new $c();
- if (method_exists($o, $a = self::$_routing['action'] . 'Action')) {
- self::$_render = $o->$a();
- }
- else {
- self::$_render = "Impossible de trouver la methode" . PHP_EOL;
- }
- echo self::$_render;
- }
- protected function render($params = [])
- {
- $f = implode(DS, [
- dirname(__DIR__),
- 'views',
- self::$_routing['controller'],
- self::$_routing['action'].'.html'
- ]);
- if (file_exists($f)) {
- $c = file_get_contents($f);
- foreach ($params as $k => $v) {
- $c = preg_replace("/\{\{\s*$k\s*\}\}/", $v, $c);
- }
- self::$_render = $c;
- }
- else {
- self::$_render = "Impossible de trouver la vue" . PHP_EOL;
- }
- echo self::$_render; // j'ai ajouté cette ligne
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement