在公共基线路线上建立路线?

我有一个共同的基本路径; 说: get /base我需要执行基本身份validation并在该路径下为所有子调用工作。 说: get /base/fooget /base/bar

查看http://www.sinatrarb.com/intro.html#Helpers建议我应该可以使用帮助程序来完成此操作。 我正在查看pass助手并在文档中触发新路由时使用call 。 但是,我读到的另一个建议是使用正则表达式IE %r{/base/?:(path)?}或其他一些动态路由。 那么怎么样:

 def '/base' # do some funky basic auth stuff here # to work with all request to this common # base path? pass end def %r{/base/?(path)?} do |path| case path when 'foo' # do something. when 'bar' # do something else. end # some kind of redirection or template rendering here: erb :template end 

有没有人以前处理过这种事情? 我想保持干爽。 当然,我不确定给定的例子在保留params方面是最好的。

有几种方法可以做到这一点(这些不是任何顺序,它们都很好):

具有前块和助手的命名空间

http://rubydoc.info/gems/sinatra-contrib/1.3.2/Sinatra/Namespace

 require 'sinatra/namespace' class App < Sinatra::Base register Sinatra::Namespace namespace "/base" do helpers do # this helper is now namespaced too authenticate! # funky auth stuff # but no need for `pass` end end before do authenticate! end get "/anything" do end get "/you" do end get "/like/here" do end end 

带有条件的命名空间

 require 'sinatra/namespace' class App < Sinatra::Base register Sinatra::Namespace set(:auth) do |*roles| # <- notice the splat here condition do unless logged_in? && roles.any? {|role| current_user.in_role? role } redirect "/login/", 303 end end end namespace "/base", :auth => [:user] do # routes… end namespace "/admin", :auth => [:admin] do # routes… end 

助手和前filter

 helpers do authenticate! # funky auth stuff # but no need for `pass` end end before '/base/*' do authenticate! end 

映射的应用

 class MyBase < Sinatra::Base helpers do authenticate! # funky auth stuff # but no need for `pass` end end before do authenticate! end get "/" do end get "/another" do end end # in rackup file map "/" do run App1 end map "/base" do # every route in MyBase will now be accessed by prepending "/base" # eg "/base/" and "/base/another" run MyBase end #… 

我不确定是否需要使用案例陈述来干预路线。 如果路线各自做了不同的事情,那么我只是将它们分开写出来,因为它更加清晰,你复制了Sinatra在匹配路线时的工作。

如何beforefilterbefore使用:

 before '/base/?*' do @foo = 'bar' end