Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Router {
- public $defaultRoute;
- protected $url, $routes = array();
- public function __construct($url) {
- $this->url = $url;
- }
- public function dispatch($method, $url, $func) {
- array_push($this->routes, $method, 0, $url, $func);
- }
- public function match($method, $regexp, $func) {
- array_push($this->routes, $method, 1, $regexp, $func);
- }
- public function run() {
- $count = count($this->routes);
- for ($i = 0; $i < $count; $i += 4) {
- if (strtoupper($this->routes[$i]) == $_SERVER['REQUEST_METHOD']) {
- if ($this->routes[$i + 1]) {
- if (preg_match($this->routes[$i + 2], $this->url,
- $matches)) {
- $func = $this->routes[$i + 3];
- return call_user_func_array($func,
- array_slice($matches, 1));
- }
- }
- else if ($this->routes[$i + 2] == $this->url) {
- $func = $this->routes[$i + 3];
- return $func();
- }
- }
- }
- if ($this->defaultRoute) {
- $func = $this->defaultRoute;
- $func();
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment