11

次のURLスキームをサポートするRESTfulスタイルのURLを構成する必要があります。

  • /親/
  • / parent / 1
  • /親/1/子供
  • / parent / 1 / children / 1

上記のそれぞれがGET/POST / PUT / DELETE関数を持つことができるように、MethodDispatcherを使用したいと思います。1回目と2回目は動作していますが、子部分のディスパッチャーを構成する方法がわかりません。私は本を​​持っていますが、それはほとんどこれをカバーしておらず、オンラインでサンプルを見つけることができません。

これが、現在MethodDispatcherを構成している方法です。

root = Root()
conf = {'/' : {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}}    

cherrypy.quickstart(root, '/parent', config=conf)

どんな助けでもいただければ幸いです。

4

2 に答える 2

9

http://tools.cherrypy.org/wiki/RestfulDispatchが探しているものかもしれません。

CherryPy 3.2 (ちょうどベータ版から出てきたばかりです) では_cp_dispatch、オブジェクト ツリーで同じことを行うために使用できる新しいメソッドが_q_lookupあり_q_resolveます。https://bitbucket.org/cherrypy/cherrypy/wiki/WhatsNewIn32#!dynamic-dispatch-by-controllersを参照してください。

于 2009-10-17T15:27:00.500 に答える
2
#!/usr/bin/env python
import cherrypy

class Items(object):
    exposed = True
    def __init__(self):
        pass

    # this line will map the first argument after / to the 'id' parameter
    # for example, a GET request to the url:
    # http://localhost:8000/items/
    # will call GET with id=None
    # and a GET request like this one: http://localhost:8000/items/1
    # will call GET with id=1
    # you can map several arguments using:
    # @cherrypy.propargs('arg1', 'arg2', 'argn')
    # def GET(self, arg1, arg2, argn)
    @cherrypy.popargs('id')
    def GET(self, id=None):
        print "id: ", id
        if not id:
            # return all items
        else:
            # return only the item with id = id

    # HTML5 
    def OPTIONS(self):                                                      
        cherrypy.response.headers['Access-Control-Allow-Credentials'] = True
        cherrypy.response.headers['Access-Control-Allow-Origin'] = cherrypy.request.headers['ORIGIN']
        cherrypy.response.headers['Access-Control-Allow-Methods'] = 'GET, PUT, DELETE'                                     
        cherrypy.response.headers['Access-Control-Allow-Headers'] = cherrypy.request.headers['ACCESS-CONTROL-REQUEST-HEADERS']

class Root(object):
    pass

root = Root()
root.items = Items()

conf = {
    'global': {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 8000,
    },
    '/': {
        'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
    },
}
cherrypy.quickstart(root, '/', conf)
于 2013-04-23T23:23:38.413 に答える