2009-10-17 36 views
10

tôi cần phải cấu hình một URL kiểu RESTful có hỗ trợ các chương trình URL sau:động URL với CherryPy MethodDispatcher

  • /phụ huynh/
  • /phụ huynh/1
  • /phụ huynh/1/trẻ em
  • /parent/1/chidren/1

Tôi muốn sử dụng MethodDispatcher để mỗi điều trên có thể có các chức năng GET/POST/PUT/DELETE. Tôi có nó làm việc cho lần đầu tiên và thứ hai, nhưng không thể tìm ra cách cấu hình điều phối cho phần trẻ em. Tôi có cuốn sách, nhưng nó hầu như không bao gồm điều này và tôi không thể tìm thấy bất kỳ mẫu trực tuyến nào.

Đây là cách tôi đã định cấu hình MethodDispatcher.

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

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

Mọi trợ giúp sẽ được đánh giá cao.

Trả lời

9

http://tools.cherrypy.org/wiki/RestfulDispatch có thể là những gì bạn đang tìm kiếm.

Trong CherryPy 3.2 (hiện đã hết beta), sẽ có một phương pháp mới _cp_dispatch bạn có thể sử dụng trong cây đối tượng để làm điều tương tự, hoặc thậm chí thay đổi traversal khi nó xảy ra, phần nào dọc theo dòng của Quixote _q_lookup_q_resolve. Xem https://bitbucket.org/cherrypy/cherrypy/wiki/WhatsNewIn32#!dynamic-dispatch-by-controllers

+1

Hoàn hảo. Đây chính xác là những gì tôi cần, nhưng tôi không thể tìm thấy nó vì tôi đã tập trung vào MethodDispatcher trong Googling của tôi. Cảm ơn. –

+2

Liên kết tới liên kết Dynamic Dispatch by Controllers đã thay đổi. Bạn có thể tìm thấy ở đây, https://bitbucket.org/cherrypy/cherrypy/wiki/WhatsNewIn32 – elarson

+0

Cảm ơn bạn đã chỉ ra phương pháp _cp_dispatch! Tôi thấy đây là một giải pháp thanh lịch cho một vấn đề tương tự trong cấu trúc ứng dụng của tôi. –

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) 
Các vấn đề liên quan