public static function handle_request() { self::setup(); SilkRoute::load_routes(); $params = array(); try { $params = SilkRoute::match_route(SilkRequest::get_requested_page()); $class_name = camelize($params['controller'] . '_controller'); if (class_exists($class_name)) { $controller = new $class_name(); } else { throw new SilkControllerNotFoundException(); } echo $controller->run_action($params['action'], $params); } catch (SilkRouteNotMatchedException $ex) { die("route not found"); } catch (SilkControllerNotFoundException $ex) { die("controller not found"); } catch (SilkViewNotFoundException $ex) { die("template not found"); } }
/** * Given a hash of key/value pairs, generate a URL for this application. * It will try and select the best URL for the situation by first going * through all the routes and seeing which is the best match. Then, any * remaining parameters are put into the querystring. * * Given the following and assuming the default route list: * @code * create_url(array('controller' => 'user', 'action' => 'list', 'some_param' => '1')) * @endcode * * Should generate: * @code * /user/list?some_param=1 * @endcode * * @param array List of parameters used to create the url * @return string * @author Ted Kulp **/ public static function create_url($params = array()) { $new_url = ''; foreach (SilkRoute::get_routes() as $one_route) { $route_params = SilkRoute::get_params_from_route($one_route->route_string); $diff = array_diff($route_params, array_keys($params)); if (!count($diff)) { //This is the first route that should work ok for the given parameters //Even if it's short, we can add the rest on via the query string $new_url = $one_route->route_string; $similar = array_intersect($route_params, array_keys($params)); foreach ($similar as $one_param) { $new_url = str_replace(":{$one_param}", $params[$one_param], $new_url); unset($params[$one_param]); } break; } } if (count($params)) { $new_url = $new_url . '?' . http_build_query($params, '', '&'); } return SilkRequest::get_calculated_url_base(true, true) . $new_url; }
/** * Automatically build routes for components. This basically makes a * /:component/:controller/:action route for each component, or a * /:component/:action route if there is only one controller. * * @return void * @author Greg Froese **/ public static function build_default_component_routes() { $components = SilkComponentManager::list_components(); foreach ($components as $component => $controllers) { foreach ($controllers as $one_controller) { $class_name = str_replace("class.", "", str_replace(".php", "", str_replace("_controller", "", $one_controller))); if (count($controllers) > 1) { $route = "/{$component}/{$class_name}/:action"; $params = array("component" => $component, "controller" => $class_name); } elseif (count($controllers) == 1 && $component == $class_name) { $route = "/{$component}/:action"; $params = array("component" => $component, "controller" => $class_name); } SilkRoute::register_route($route, $params); } } }
<?php SilkRoute::register_route("/:controller/:action/:id"); SilkRoute::register_route("/:controller/:action", array("id" => '')); SilkRoute::register_route("/:controller", array("id" => '', 'action' => 'index'));