Browse files

- Implemented new routing system, with docs.

- Renamed default controller to 'home'.
- Setup an 'admin' area on the site now that maps to the admin controller within modules.
  • Loading branch information...
lonnieezell committed Sep 3, 2013
1 parent 759164b commit c329d78beeeb265c60b8671b9d8f847485e76f11
@@ -38,13 +38,18 @@
$route['default_controller'] = "welcome";
$route['default_controller'] = "home";
$route['404_override'] = '';
// User Logins
$route['login'] = 'users/login';
$route['logout'] = 'users/logout';
$route['register'] = 'users/register';
// Admin area
Route::area('admin', 'admin');
$route = Route::map($route);
/* End of file routes.php */
/* Location: ./application/config/routes.php */
@@ -0,0 +1,14 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
protected $view_folder = 'admin';
public function index()
@@ -1,6 +1,6 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller {
class Home extends MY_Controller {
* Index Page for this controller.
@@ -20,7 +20,6 @@ class Welcome extends MY_Controller {
public function index()
@@ -22,6 +22,7 @@ class MY_Controller extends CI_Controller {
protected $model_file = NULL;
private $use_view = '';
protected $view_folder = '';
private $use_layout = '';
protected $external_scripts = array();
@@ -150,6 +151,11 @@ protected function render($data=array())
// Calc our view name based on current method/controller
$view = !empty($this->use_view) ? $this->use_view : $this->router->fetch_class() .'/'. $this->router->fetch_method();
if (!empty($this->view_folder))
$view = $this->view_folder .'/'. $view;
// Merge any saved vars into the data
$data = array_merge($data, $this->vars);
@@ -8,16 +8,16 @@ class MY_Router extends HMVC_Router {
* Ensures that the Route class' routes
* are added to the routes in the parent classes.
public function _parse_routes()
$routes = Route::$routes;
// public function _parse_routes()
// {
// $routes = Route::$routes;
$this->routes = array_merge($this->routes, $routes);
// $this->routes = array_merge($this->routes, $routes);
return parent::_parse_routes();
// return parent::_parse_routes();
// }
// //--------------------------------------------------------------------
@@ -47,8 +47,38 @@ class Route {
// Holds the 'areas' of the site.
public static $areas = array();
public static $route;
public function init(&$route=null)
self::$route = $route;
* Attaches the routes in the system to the global $config array.
* This method should be called in the routes config file after
* specifying
* @return [type] [description]
public function map($route)
foreach (self::$routes as $from => $to)
$route[$from] = str_replace('{default_controller}', $route['default_controller'], $to);
return $route;
* A single point to do the actual routing. Can be used in place of
* the $route array, if desired. Primarily used by our HTTP-verb based
@@ -334,15 +364,21 @@ public static function resources($name, $options=array())
public static function area($area, $controller)
global $route;
// Save the area so we can recognize it later.
self::$areas[$area] = $controller;
// Create routes for this area.
self::create($area .'/(:any)/(:any)/(:any)/(:any)/(:any)/(:any)', '$1/'. $controller .'/$2/$3/$4/$5/$6');
self::create($area .'/(:any)/(:any)/(:any)/(:any)/(:any)', '$1/'. $controller .'/$2/$3/$4/$5');
self::create($area .'/(:any)/(:any)/(:any)/(:any)', '$1/'. $controller .'/$2/$3/$4');
self::create($area .'/(:any)/(:any)/(:any)', '$1/'. $controller .'/$2/$3');
self::create($area .'/(:any)/(:any)', '$1/'. $controller .'/$2');
self::create($area .'/(:any)', '$1/'. $controller);
// Setup a home controller with the name of the area and the default controller
self::create($area, $area .'/{default_controller}');
@@ -0,0 +1,157 @@
# Routes in SprintPHP

Sprint extends the core routing capbilities of CodeIgniter's router to provide much more flexible types of routing. This is heavily based off of Jamie Rumbelow's excellent [Pigeon library](

All of the standard [CodeIgniter Routing]( capabilities still exist. In addition, you can:

- Route to module controllers
- Named routes for easier access throughout your other modules
- HTTP verb-based routing
- Restful resources
- Scoped routes

## Named Routes

Named routes make referring to locations in your app simple and less error-prone. If used well, you could completely restructure your application by simply changing the route names. You may specify a name for a route like:

Route::with_name('profile', 'users/profile');

The name must be the first part of the URI, and is separated from the uri string by a double colon (::). In this example, the name is <tt>profile</tt> and the uri is <tt>users/edit_profile</tt>.

Now you can use the route's name when generaint URL's or redirects.

$url = Route::named('profile');


You can get the full site url for a named route with the following function:

$url = Route::named_url('profile');

Would return:

If no name had been declared for that route in the past, then NULL would be returned by this function.

## HTTP Verb Routing

To make building REST-based routing simpler and more consistent, you can use the


This function will automatically create RESTful resources for the common HTTP verbs. In this example, <tt>controller_name</tt> is the name of the controller you want to map the resources to. If you controller is named <tt>photos</tt>, you would call it like:


If the <tt>photos</tt> controller is part of the <tt>Gallery</tt> module, then you would route it like:


This would map the resources to the <tt>Photos</tt> controller, like:

HTTP Verb | Path | action | used_for
GET | /photos | index | display a list of photos
GET | /photos/new | new | return an HTML form for creating a new photo
POST | /photos | create | create a new photo
GET | /photos/{id} | show | display a specific photo
GET | /photos/{id}/edit | edit | return the HTML for editing a single photo
PUT | /photos/{id} | update | update a specific photo
DELETE | /photos/{id} | destroy | delete a specific photo

You can also set a single verb-based route with any of the route methods:

Route::get('from', 'to');
Route::post('from', 'to');
Route::put('from', 'to');
Route::delete('from', 'to');
Route::head('from', 'to');
Route::patch('from', 'to');
Route::options('from', 'to');

These routes will then only be available when the corresponding HTTP verb is used to initiate the call.

## Customizing Resourceful Routes

While the standard naming convention provided by the <tt>resources</tt> Route method will often serve you well, you may find that you need to customize the route
to easily control where your URL's route to.

### Specifying a controller to use

You can pass an array of options into the <tt>resources</tt> method as the second parameter. By specifying a <tt>controller</tt> key, you will tell the router to replace all instances of the original route with the defined controller, like:

Route::resources('photos', array('controller' => 'images'));

Will recognize incoming paths beginning with <tt>/photos</tt> but will route to the <tt>images</tt> controller:

### Specifying the module to use

You can also specify a module to use in the options array by passing a <tt>module</tt> key. This is helpful when the module and controller share different names.

Route::resources('photos', array('module' => 'gallery', 'controller' => 'images'));

Will recognize incoming paths beginning with <tt>/photos</tt> but will route to the <tt>gallery/images</tt> module and controller.

### Constraining the {id} format

By default, the {id} used in the routing allows any letter, lower- or upper-case, any digit (0-9), a dash (-) and an underscore(_). If you need to restrict the {id} to another format, you may use the <tt>constraint</tt> option to pass a new, valid, format string:
Route::resources('photos', array('constraint' => '(:num)'));
Would restrict the {id} to be only numerals, while:
Route:resources('photos', array('constraint' => '([A-Z][A-Z][0-9]+)'));
would restrict the {id} to be something like RR27.
## Grouping Routes
Scoped routes allow you to group routes under a specific area of the website. This is commonly done to group certain routes into an admin area of your site.
Route::group('admin', function (){
Route::create('login', 'users/login');
Would create the following resources:
admin/login = users/login
as well as mapping the following resources:
HTTP Verb | Path | action | used_for
GET | /admin/users | index | display a list of users
GET | /admin/users/new | new | return an HTML form for creating a new user
POST | /admin/users | create | create a new user
GET | /admin/users/{id} | show | display a specific user
GET | /admin/users/{id}/edit | edit | return the HTML for editing a single user
PUT | /admin/users/{id} | update | update a specific user
DELETE | /admin/users/{id} | destroy | delete a specific user
Grouping routes does not enforce any specific controller naming scheme. However, you might want to separate your grouped methods into a different controller within your module for consistency.
## Routing Areas
When you want to map an entire area of your website to map to a specific controller, you can use the <tt>area</tt> method.
Route::area('area', 'controller');
For example, to map all /admin routes to the 'admin' controller in any module, you would do something like:
Route::area('admin', 'admin');
This would create the following routes:
admin/(:any)/(:any)/(:any)/(:any)/(:any)/(:any) => $1/admin/$2/$3/$4/$5/$6
admin/(:any)/(:any)/(:any)/(:any)/(:any) => $1/admin/$2/$3/$4/$5
admin/(:any)/(:any)/(:any)/(:any) => $1/admin/$2/$3/$4
admin/(:any)/(:any)/(:any) => $1/admin/$2/$3
admin/(:any)/(:any) => $1/admin/$2
admin/(:any) => $1/admin
admin => admin/{default_controller}
@@ -0,0 +1 @@
<h1>Welcome, Admin</h1>

0 comments on commit c329d78

Please sign in to comment.