Class Misago\ActionController\Routing\ResourceRoutes

RESTful routes

A resource is a pair of controller/model with a REST logic in routes. Declaring a resource will create a bunch of named routes.

See resource for additional help.

Attention: in RESTful routes :id must be an integer.

Nested routes

Relations are great, but often come with ugly URL like /tickets/new?event_id=123. With nested routes you may create better URL and help methods. For instance:

$map->resource('event', array('has_many' => 'tickets')); 

This will create named routes like:

event_tickets      /event/:event_id/tickets/new
new_event_ticket   /event/:event_id/tickets/:id
edit_event_ticket  /event/:event_id/tickets/:id/edit
etc. 

You may also achieve nested resources the following ways:

# using +has_many+:
$map->resource('event', array('has_many' => 'tickets'));

# using closures:
$map->resource('event', function($event) {
  $event->resource('tag');
});

# using +path_prefix+ (not recommended):
$map->resource('event');
$map->resource('ticket', array('path_prefix' => 'event/:id')); 

Inheritence

Extends:
Misago

Methods

Public instance methods

resource($name, $options=array(), $closure=null)

Singleton resource. Resource name must always be singular, but the controller & index use the plural form.

$map->resource('account'); 

This will create the following named routes:

accounts        GET     /accounts          => AccountsController::index()
new_account     GET     /account/new       => AccountsController::neo()
create_account  POST    /account           => AccountsController::create()
edit_account    GET     /account/:id/edit  => AccountsController::edit()
show_account    GET     /account/:id       => AccountsController::show()
update_account  PUT     /account/:id       => AccountsController::update()
delete_account  DELETE  /account/:id       => AccountsController::delete() 

Available options:

  • as - use this name for the path instead
  • collection - a hash of additional collection methods {action => method}
  • controller - force controller's name (defaults to plural name)
  • except - list of routes to skip (eg: ['update', 'delete'])
  • has_one - declare a nested singleton resource
  • has_many - declare a nested collection resource
  • name_prefix - particular prefix for routes' name
  • member - same as collection but applies to a particular id
  • only - list of routes to generate (eg: ['index', 'show'])
  • path_prefix - a particular prefix for routes' path
  • singular - force singular name

FIXME: 'only' causes 'collection' & 'member' to not generate routes!