如何在 Sinatra 中混合路线以获得更好的结构 [英] How mix in routes in Sinatra for a better structure
本文介绍了如何在 Sinatra 中混合路线以获得更好的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我没有发现如何从另一个模块混合路由,如下所示:
I found nothing about how I can mix-in routes from another module, like this:
module otherRoutes
get "/route1" do
end
end
class Server < Sinatra::Base
include otherRoutes
get "/" do
#do something
end
end
这可能吗?
推荐答案
您不需要对 Sinatra 进行include.您可以将扩展与 register 一起使用.
You don't do include with Sinatra. You use extensions together with register.
即在单独的文件中构建您的模块:
I.e. build your module in a separate file:
require 'sinatra/base'
module Sinatra
module OtherRoutes
def self.registered(app)
app.get "/route1" do
...
end
end
end
register OtherRoutes # for non modular apps, just include this file and it will register
end
然后注册:
class Server < Sinatra::Base
register Sinatra::OtherRoutes
...
end
从文档中并不清楚这是非基本 Sinatra 应用程序的方法.希望能帮到其他人.
It's not really clear from the docs that this is the way to go for non-basic Sinatra apps. Hope it helps others.
这篇关于如何在 Sinatra 中混合路线以获得更好的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文