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 add($method, $rule, $func) {
- array_push($this->_routes, $method, $rule, $func);
- }
- public function get($rule, $func) {
- array_push($this->_routes, 'GET', $rule, $func);
- }
- public function post($rule, $func) {
- array_push($this->_routes, 'POST', $rule, $func);
- }
- public function put($rule, $func) {
- array_push($this->_routes, 'PUT', $rule, $func);
- }
- public function delete($rule, $func) {
- array_push($this->_routes, 'DELETE', $rule, $func);
- }
- public function process() {
- $n = count($this->_routes);
- for ($i = 0; $i < $n; $i += 3) {
- $method = $this->_routes[$i];
- if (strtoupper($method) == $_SERVER['REQUEST_METHOD']) {
- $rule = $this->_routes[$i + 1];
- $func = $this->_routes[$i + 2];
- @$is_regexp = preg_match($rule, '') !== false;
- if ($is_regexp) {
- if (preg_match($rule, $this->_url, $matches)) {
- return call_user_func_array($func, array_slice($matches, 1));
- }
- }
- else if ($rule == $this->_url) {
- return call_user_func($func);
- }
- }
- }
- if ($this->defaultRoute) {
- $func = $this->defaultRoute;
- call_user_func($func);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment