CherryPy中类似CodeIgniter的路由

我来自PHP框架,我喜欢它们的一个方面是路由对我来说有点照顾:我可以将所有控制器放在目录
controllers
中,当用户访问
http://www.example.com/posts/delete/12
时它会自动调用
Posts::delete(12)
。我意识到我可以使用带有CherryPy的路由,但我对文档的限制有点恼火 - 我怎么没有关于如何格式化我的类名(我应该称之为PostsController()吗?它甚至关心吗?),使用
routes.mapper.connect()
routes.connect()
,以及调用默认路径时会发生什么(
/:controller/:action/:id
)。 我真的很想使用Python,但我不想定义每一条路线。有人可以指向我一个关于如何使用Routes的Python web-framework newb教程,或者只是解释一下如何构建一个CherryPy web-app,这样我就可以有几个Routes布局了
d = cherrypy.dispatch.RoutesDispatcher()
d.mapper.connect('main', '/:controller/:action', controller='root', action='index')
d.mapper.connect('main', '/:controller/:action/:id', controller='root', action='index')
它会为我处理它吗?谢谢。     
已邀请:
简单的方法是使用
cherrypy.tree.mount
安装控制器对象。控制器的结构将为您提供基本路线。 例如:
import cherrypy

class AppRoot:
    def index(self):
        return "App root's index"
    index.exposed = True

    controller1 = Controller1Class()
    # controller2 = Controller2Class(), etc.

class Controller1Class:

     def index(self):
         return "Controller 1's index"
     index.exposed = True

     def action1(self, id):
         return "You passed %s to controller1's action1" % id      
     action1.exposed = True

     # def action2(self, id): etc...

# ... the rest of the config stuff ...

cherrypy.tree.mount(AppRoot(), '/')  

# ... the rest of the startup stuff....
调用以下URI将调用以下方法: http://mysite.com/index - >
AppRoot::index()
http://mysite.com/controller1 - >
Controller1Class::index()
http://mysite.com/controller1/action1 - >
Controller1Class::action1()
http://mysite.com/controller1/action1/40 - >
Controller1Class::action1("40")
也可以看看: http://mark.biek.org/blog/2009/07/basic-routing-with-cherrypy/ http://www.cherrypy.org/wiki/PageHandlers     

要回复问题请先登录注册