Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Router{
- public $default;
- public $rule;
- public $path;
- public $baseInclusionPath;
- function Route( $default='more/404' ) {
- $this->default = $default;
- $this->baseInclusionPath = 'controllers/';
- //> $_SERVER['REQUEST_URI'] sample: /page/file?abc
- $this->path = $_SERVER['REQUEST_URI'];
- $this->path = substr($this->path,1); //> Removing leading slash
- }
- public function add( $pattern, $app = null ) {
- $this->rule[$pattern] = $app;
- return $this;
- }
- public function dispatch() {
- foreach($this->rule as $pattern=>$app) {
- if (preg_match('#^'.$pattern.'$#i',$this->path,$match)) {
- print_r($match);
- $this->run($app,$match);
- return true;
- }
- }
- //> Nothing Matched run 404 error
- $this->run($this->default);
- }
- private function run($app,$match=array()) {
- //> First entry is all the string matched
- array_shift($match);
- if ($app==null) {
- $app = $match[0];
- }
- require($this->baseInclusionPath.$app.'.php');
- //> Remove the controller name
- array_shift($match);
- $method = $match[0];
- //> Remove the method name
- array_shift($match);
- $app = 'Controller'.ucfirst($app);
- if (function_exists($app)) {
- call_user_func_array($app,$match);
- } else {
- $app = new $app;
- call_user_func_array(array($app, $method), $match);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement