for empty urls ("/"), only root routes (added with addRootRoute) will be matched against. a specific route example to only match ids that are valid numbers: HalfMoon\Router::addRoute(array( "url" => "posts/:id", "controller" => "posts", "action" => "show", "conditions" => array("id" => '/^\d+$/'), )); a root route to match "/": HalfMoon\Router::addRootRoute(array( "controller" => "posts" )); another root route on a specific virtual host to map to a different action (this would have to be defined before the previous root route, since the previous one has no conditions and would match all root urls): HalfMoon\Router::addRootRoute(array( "controller" => "posts", "action" => "devindex", "conditions" => array("hostname" => "dev"), )); */ /* generic catch-all route to match everything else */ HalfMoon\Router::addRoute(array("url" => ":controller/:action/:id"));
/** * @depends testSetupRoutes */ public function testZeroRouting() { $route = HalfMoon\Router::routeRequest($this->request_for("http://www.example2.com/zero_test/0")); $this->assertEquals("zero_test", $route["controller"]); $this->assertEquals("show", $route["action"]); $this->assertEquals("0", $route["id"]); }
require_once HALFMOON_ROOT . "/config/boot.php"; } /* autoload controllers and helpers as needed */ function halfmoon_autoload($class_name) { if (preg_match("/^([A-Za-z0-9_]+)(Controller|Helper)\$/", $class_name, $m)) { $file = HALFMOON_ROOT . "/"; /* Controller -> controllers/ */ $file .= strtolower($m[2]) . "s/"; /* CamelCase -> camel_case_controller.php */ $words = preg_split('/(?<=\\w)(?=[A-Z])/', $m[1]); $file .= strtolower(join("_", $words)) . "_" . strtolower($m[2]) . ".php"; if (file_exists($file)) { require_once $file; } } } spl_autoload_register("halfmoon_autoload", false, false); if (defined("PHP_ACTIVERECORD_ROOT")) { HalfMoon\Config::initialize_activerecord(); } /* bring in any post-framework but pre-route code */ if (file_exists($f = HALFMOON_ROOT . "/config/application.php")) { require_once $f; } /* bring in the route table and route our request */ if (file_exists(HALFMOON_ROOT . "/config/routes.php")) { HalfMoon\Router::initialize(function ($router) { require_once HALFMOON_ROOT . "/config/routes.php"; }); }