emvc

Routing : Resources

Resource routing declares routes to a resourceful controller. These routes map HTTP methods to a controller's actions, according to REST conventions. Resource routing is the preferred routing mechanism in a Locomotive application.

Collection Resources

Declaring this route:

this.resources('photos');

will result in the following routes being mapped to PhotosController:

Method Path Action
GET /photos index
GET /photos/new new
POST /photos create
GET /photos/:id show
GET /photos/:id/edit edit
PUT /photos/:id update
DELETE /photos/:id destroy

Additionally, the following routing helpers will be declared and available in controllers and views:

Helper Returns
photosPath() /photos
photoPath(id) /photos/123
newPhotoPath() /photos/new
editPhotoPath(id) /photos/123/edit

Each of these path routing helpers has a corresponding URL routing helper (such as photosURL()), that returns an absolute URL, including scheme and host. Using these helpers, as opposed to hardcoding paths, makes an application easier to maintain as routes change.

Only and Except

If you have resources that only implement a subset of the CRUD operations, you can limit the routes to just the actions that are supported:

this.resources('bands', { only: [ 'index', 'show', 'edit', 'update' ] });

which is equivalent to:

this.resources('bands', { except: [ 'new', 'create', 'destroy' ] });

Singleton Resources

An application may contain singleton resources that are not referenced using an ID. For example, /account is often used to show account details for the logged in user (rather than /account/:id).

In this case, a singleton resource route can be declared:

this.resource('account');

resulting in the following routes being mapped to AccountController:

Method Path Action
GET /account/new new
POST /account create
GET /account show
GET /account/edit edit
PUT /account update
DELETE /account destroy

Additionally, the following routing helpers will be declared and available in controllers and views:

Helper Returns
accountPath() /account
newAccountPath() /account/new
editAccountPath() /account/edit

Each of these path routing helpers has a corresponding URL routing helper.