Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Rou {
- var $core;
- var $config;
- var $pathinfo;
- var $main;
- var $app;
- public function __construct(){
- $this->core = new Cor;
- }
- public function init($config){
- $pathinfo = array_slice(explode('/', str_replace($config['url_suffix'], '', $_SERVER['PATH_INFO'])), 1);
- if(file_exists("controllers/{$config['default_route']}.php")) require "controllers/{$config['default_route']}.php";
- if(file_exists("controllers/{$pathinfo[0]}.php")) require "controllers/{$pathinfo[0]}.php";
- if(class_exists($config['default_route'])) $main = new $config['default_route'];
- if(class_exists(String::lower($pathinfo[0]))) $app = new $pathinfo[0];
- if(isset($config)) $this->config = $config;
- if(isset($pathinfo)) $this->pathinfo = $pathinfo;
- if(isset($main)) $this->main = $main;
- if(isset($app)) $this->app = $app;
- $this->global_func('before');
- if($pathinfo[0] == '' && in_array('index', get_class_methods($main))){ #Is this is the root of the website?
- $main->index(); #Show the index of the default route
- } elseif(isset($pathinfo[1]) && isset($app) && in_array($pathinfo[1], get_class_methods($app))){ #Are we inside of a route?
- call_user_func_array(array($app, $pathinfo[1]), array_slice($pathinfo, 1)); #Yep, let's call that function.
- } elseif(!isset($pathinfo[1]) && isset($app) && in_array('index', get_class_methods($app))) { #Are we in a route and is there an index on this route?
- $app->index(); #Show it
- } elseif(in_array($pathinfo[0], get_class_methods($main))){ #Not anywhere else? Is it in the main route?
- call_user_func_array(array($main, $pathinfo[0]), array_slice($pathinfo, 1)); #Yes! let's call it.
- } else { #Nothing else :(
- $this->core->error(404);
- }
- $this->global_func('after');
- }
- private function global_func($func){
- if(class_exists($this->config['default_route']) && in_array($func, get_class_methods($this->main))) $this->main->$func();
- if(isset($this->app) && class_exists($this->pathinfo[0]) && in_array($func, get_class_methods($this->app))) $this->app->$func();
- }
- }
Add Comment
Please, Sign In to add comment