= Routes = '''Routes''' are the things which translate a URL into a [wiki:Controllers controller] and [wiki:Actions action]. They can also define parameters that are available to the controller, such as an ID or other identifier. == Definitions == Routes are defined in the file [source:trunk/config/routes.php config/routes.php]. The default routes map a URL directly to a controller and action. For example: {{{http://example.com/foo/bar}}} maps to the {{{bar}}} action of the {{{FooController}}}. If no action is specified in the route, the [source:trunk/system/Router.php Router] will assume the action is {{{index}}}. == Creating Routes == ''NB: This area is under active development and '''will''' change! Also, I'm sorry about how I did this initially.'' Routes are (currently) defined as regular expressions and matching route information. The regular expression is the key of the global array {{{$routes}}}, and the route information is the value, stored as an array. {{{ #!php bar() // FooController->params['id'] = 23; // FooController->params['format'] = 'htm'; }}} === Cascading === Importantly, routes are checked in order. That is, if the first defined route matches the current URL, no other routes are checked. That means static routes must come first, and more liberal routes must come last. The default route must be the last route defined. === Default Route === The default route has a special regular expression: {{{_default}}}. It must be the last route defined. If no other route matches, the default route will be used. Rather than making the default map to your index page, consider mapping {{{_default}}} to a 404 page, perhaps returning a 404 header, and mapping the empty route ({{{'$'}}}) to the index. == Examples == {{{ #!php about() // An ID-based user profile page: $routes['user/:id'] = array('user/profile', 'id'); // maps http://example.com/user/24 to UserController->profile() // UserController->params['id'] = 24 // A name-based user profile page: $routes['user/([a-z]\w{2,})/?'] = array('user/profile', 'name'); // maps http://example.com/user/examplename to UserController->profile() // UserController->params['name'] = 'examplename'; }}}