Route

Bonfire includes a Route library to supplement the functionality provided by CodeIgniter’s (and the Wiredesignz HMVC) router(s). Additional information may be available in the section on Improved Routes.

Methods

map([$routes])

Combines the routes which have been defined in the Route class with the passed $routes. This is intended to be used after all routes have been defined to merge CI’s default $route array with routes defined with this Route library.

Note that in CI3 the 'translate_uri_dashes' setting should be set after calling this method.

Example:

$route['default_controller'] = 'home';
Route::resource('posts');
$route = Route::map($route);

$routes is an array of routes to merge with the routes defined by the Route library. Usually, the variable passed into this method will be the CI $route array.

If a ‘default_controller’ route is not set in the passed $routes, it will be set to 'home' (or whatever value is defined in the Route library’s protected $default_home property).

Returns the merged array, which should be assigned to the CI $route array.

any($from, $to[, $options[, $nested = false]])

Create a basic route. In its most basic form, this method will create a route similar to defining a CI route in the form $route[$from] = $to;.

HTTP Verb-based routing

get($from, $to[, $options[, $nested = false]])

Creates a route which will only be accessible when the $_SERVER['REQUEST_METHOD'] indicates that the current request is a GET request. The arguments match those in the any() method.

post($from, $to[, $options[, $nested = false]])

Creates a route which will only be accessible when the $_SERVER['REQUEST_METHOD'] indicates that the current request is a POST request. The arguments match those in the any() method.

put($from, $to[, $options[, $nested = false]])

Creates a route which will only be accessible when the $_SERVER['REQUEST_METHOD'] indicates that the current request is a PUT request. The arguments match those in the any() method.

delete($from, $to[, $options[, $nested = false]])

Creates a route which will only be accessible when the $_SERVER['REQUEST_METHOD'] indicates that the current request is a DELETE request. The arguments match those in the any() method.

head($from, $to[, $options[, $nested = false]])

Creates a route which will only be accessible when the $_SERVER['REQUEST_METHOD'] indicates that the current request is a HEAD request. The arguments match those in the any() method.

patch($from, $to[, $options[, $nested = false]])

Creates a route which will only be accessible when the $_SERVER['REQUEST_METHOD'] indicates that the current request is a PATCH request. The arguments match those in the any() method.

options($from, $to[, $options[, $nested = false]])

Creates a route which will only be accessible when the $_SERVER['REQUEST_METHOD'] indicates that the current request is an OPTIONS request. The arguments match those in the any() method.

block($paths)

Prevents access to an array of routes by setting each of the supplied values in the $paths array to an empty path.

If $paths is not an array, this method will do nothing.

context($name[, $controller[, $options]])

Provides a method for assigning controllers in modules to an area of the site based on the name of the controller.

Note that $options can be passed as the second argument if the $controller argument is not needed.

named($name)

Returns the $from portion of a route which was previously saved as $name. Returns null if a route was not found with the given $name.

prefix($name, Closure $callback)

Prefix a set of routes (defined in the $callback closure) with the $name prefix. All routes defined in the closure will be defined with the $from portion of the route prefixed with $name (e.g. 'users' becomes 'api/users' if $name is 'api').

reset()

Resets the internal state of the Route library, eliminating any routes which have not already been output.

resources($name[, $options[, $nested = false]])

Creates a pre-defined set of HTTP-verb based routes for the $name controller.

Example:

Route::resources('photos');

Generates the following routes:

Verb Path Action used for
GET /photos index displaying a list of photos
GET /photos/new create_new return an HTML form for creating a photo
GET /photos/{id}/edit edit return the HTML form for editing a single photo
GET /photos/{id} show display a specific photo
POST /photos create create a new photo
PUT /photos/{id} update update a specific photo
DELETE /photos/{id} delete delete a specific photo