/** * Figure out where the user is trying to get to and route them to the appropriate controller/action. //剖析網址的規則, 取得 Controller/Action */ public function router() { $r = new Router(); // Configure the routes, where the user should go when they access the // specified URL structures. Default controller and action is set in the config.php file. $r->map('/', array('controller' => ROUTER_DEFAULT_CONTROLLER, 'action' => ROUTER_DEFAULT_ACTION)); // Load instructions for basic routing and send the user on their way! $r->default_routes(); $r->execute(); // Extracting info about where the user is headed, in order to match the // URL with the correct controllers/actions. $controller = $r->controller; $model = $r->controller_name; $action = $r->action; $id = $r->id; $params = $r->params; // Returns an array(...) $matched = $r->route_found; // Bool, where True is if a route was found. if ($matched) { // If going to a site page, treat it in special manner, otherwise load // the appropriate controller/action and pass in the variables and // parameters specified by the URL. $controller_file = BASE_DIR . "/controller/{$controller}.php"; if ($controller == "site") { $site = new Site(); $site->login(); $site->home(); } elseif (file_exists($controller_file)) { include_once $controller_file; $site = new Site(); $site->login(); ${$controller} = new $model(); if (method_exists(${$controller}, $action)) { ${$controller}->controller['controller'] = $controller; ${$controller}->controller['action'] = $action; ${$controller}->id = $id; ${$controller}->params = $params; ${$controller}->{$action}(); } else { Site::load_page('error'); } } else { Site::load_page('error'); } } else { Site::home(); } }
/** * Figure out where the user is trying to get to and route them to the * appropriate controller/action. */ function router() { // Create a new Router instance. $r = new Router(); // Configure the routes, where the user should go when they access the // specified URL structures. Default controller and action is set in the // config.php file. $r->map('/', array('controller' => ROUTER_DEFAULT_CONTROLLER, 'action' => ROUTER_DEFAULT_ACTION)); $r->map('/user', array('controller' => 'user', 'action' => 'index')); $r->map('/login', array('controller' => 'user', 'action' => 'login')); $r->map('/logout', array('controller' => 'user', 'action' => 'logout')); $r->map('/signup', array('controller' => 'user', 'action' => 'register')); $r->map('/users/:id', array('controller' => 'users'), array('id' => '[\\d]{1,8}')); // Load instructions for basic routing and send the user on their way! $r->default_routes(); $r->execute(); // Extracting info about where the user is headed, in order to match the // URL with the correct controllers/actions. $controller = $r->controller; $model = $r->controller_name; $action = $r->action; $id = $r->id; $params = $r->params; // Returns an array(...) $matched = $r->route_found; // Bool, where True is if a route was found. if ($matched) { // If going to a site page, treat it in special manner, otherwise load // the appropriate controller/action and pass in the variables and // parameters specified by the URL. if ($controller == "site") { $site = new Site(); $site->load_page($action); } elseif (file_exists(LIB_DIR . '/controllers/' . $controller . '.php')) { ${$controller} = new $model(); if (method_exists(${$controller}, $action)) { ${$controller}->{$action}($id, $params[0], $params[1]); } else { Site::load_page('error'); } } else { Site::load_page('error'); } } else { Site::load_page('home'); } }
<?php $router = new Router(); // create router instance $router->map('/', array('controller' => 'home')); // Repositories // Completed $router->map('/:project', array('controller' => 'projects', 'action' => 'show', 'branch' => 'master'), array('project' => '[\\w_-]+(?:\\.git)?')); // Completed $router->map('/:project/tree', array('controller' => 'projects', 'action' => 'show', 'branch' => 'master'), array('project' => '[\\w_-]+(?:\\.git)?')); // Completed $router->map('/:project/tree/:branch', array('controller' => 'projects', 'action' => 'show'), array('project' => '[\\w_-]+(?:\\.git)?', 'branch' => '[\\w_-]+')); // Completed $router->map('/:project/tree/:branch/:filepath', array('controller' => 'projects', 'action' => 'show'), array('project' => '[\\w_-]+(?:\\.git)?', 'branch' => '[\\w_-]+', 'filepath' => '.*')); $router->map('/:project/branch_redirect', array('controller' => 'projects', 'action' => 'branch_redirect'), array('project' => '[\\w_-]+(?:\\.git)?')); // Completed $router->map('/:project/commit/:commit', array('controller' => 'commits', 'action' => 'show'), array('project' => '[\\w_-]+(?:\\.git)?', 'commit' => '[\\w]+')); // Completed $router->map('/:project/commits', array('controller' => 'commits', 'action' => 'index', 'branch' => 'master'), array('project' => '[\\w_-]+(?:\\.git)?')); // Completed $router->map('/:project/commits/:branch', array('controller' => 'commits', 'action' => 'index'), array('project' => '[\\w_-]+(?:\\.git)?', 'branch' => '[\\w_-]+')); //Files $router->map('/:project/blob/:branch/:filepath', array('controller' => 'projects', 'action' => 'blob'), array('project' => '[\\w_-]+(?:\\.git)?', 'branch' => '[\\w_-]+', 'filepath' => '.*')); $router->map('/:project/raw/:branch/:filepath', array('controller' => 'projects', 'action' => 'raw'), array('project' => '[\\w_-]+(\\.git)?', 'branch' => '[\\w_-]+', 'filepath' => '.*')); $router->map('/:project/blame/:branch/:filepath', array('controller' => 'projects', 'action' => 'blame'), array('project' => '[\\w_-]+(\\.git)?', 'branch' => '[\\w_-]+', 'filepath' => '.*')); $router->map('/:project/download/:branch/:filepath', array('controller' => 'projects', 'action' => 'download'), array('project' => '[\\w_-]+(\\.git)?', 'branch' => '[\\w_-]+', 'filepath' => '.*')); $router->default_routes(); $router->execute();