resource

resource
  对于开发RESTful类型的服务,Routes提供了快捷的方法定义对资源resource的url映射:
  map.resource("volume", "volumes")
  上面一行主要等效于下面的代码:

    map.connect("volumes", "/volumes",
      controller="volumes", action="create",
      conditions=dict(method=["POST"]))
    map.connect("volumes", "/volumes",
      controller="volumes", action="index",
      conditions=dict(method=["GET"]))
    map.connect("formatted_volumes", "/volumes.{format}",
      controller="volumes", action="index",
      conditions=dict(method=["GET"]))
    map.connect("new_volume", "/volumes/new",
      controller="volumes", action="new",
      conditions=dict(method=["GET"]))
    map.connect("formatted_new_volume", "/volumes/new.{format}",
      controller="volumes", action="new",
      conditions=dict(method=["GET"]))
    map.connect("/volumes/{id}",
      controller="volumes", action="update",
      conditions=dict(method=["PUT"]))
    map.connect("/volumes/{id}",
      controller="volumes", action="delete",
      conditions=dict(method=["DELETE"]))
    map.connect("edit_volume", "/volumes/{id}/edit",
      controller="volumes", action="edit",
      conditions=dict(method=["GET"]))
    map.connect("formatted_edit_volume", "/volumes/{id}.{format}/edit",
      controller="volumes", action="edit",
      conditions=dict(method=["GET"]))
    map.connect("volume", "/volumes/{id}",
      controller="volumes", action="show",
      conditions=dict(method=["GET"]))
    map.connect("formatted_volume", "/volumes/{id}.{format}",
      controller="volumes", action="show",
      conditions=dict(method=["GET"]))

  对于资源resource来说,主要有四个route名称,对应六个动作:
    GET => show
    new form => GET
    new => POST
    edit form => GET
    edit => PUT
    delete => DELETE
  可以使用下面的例子,查看到resource()创建的路由

from routes import Mapper
map = Mapper()
map.resource('volume', 'volumes')

for i in range(len(map.matchlist)):
    route = map.matchlist[i]
    print('[%r] %r' % (i, route.name))
    print('\t regpath: ' + route.regpath)
    print('\t method: %r' % route.conditions['method'])

  程序输出:

[0] None
regpath: /volumes.%(format)s
method: ['POST']
[1] None
regpath: /volumes
method: ['POST']
[2] 'formatted_volumes'
regpath: /volumes.%(format)s
method: ['GET']
[3] 'volumes'
regpath: /volumes
method: ['GET']
[4] 'formatted_new_volume'
regpath: /volumes/new.%(format)s
method: ['GET']
[5] 'new_volume'
regpath: /volumes/new
method: ['GET']
[6] None
regpath: /volumes/%(id)s.%(format)s
method: ['PUT']
[7] None
regpath: /volumes/%(id)s
method: ['PUT']
[8] None
regpath: /volumes/%(id)s.%(format)s
method: ['DELETE']
[9] None
regpath: /volumes/%(id)s
method: ['DELETE']
[10] 'formatted_edit_volume'
regpath: /volumes/%(id)s/edit.%(format)s
method: ['GET']
[11] 'edit_volume'
regpath: /volumes/%(id)s/edit
method: ['GET']
[12] 'formatted_volume'
regpath: /volumes/%(id)s.%(format)s
method: ['GET']
[13] 'volume'
regpath: /volumes/%(id)s
method: ['GET']

参考资料:
  RESTful services