在共同的基础路线上建立路线?

时间:2013-03-29 01:13:59

标签: ruby routes sinatra

我有一个共同的基本路径;说:get /base我需要执行基本身份验证并为该路径下的所有子调用工作。说: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方面是最好的。

1 个答案:

答案 0 :(得分:9)

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

具有前块和助手

的命名空间

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

助手和前过滤器

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"
  # e.g. "/base/" and "/base/another"
  run MyBase
end
#…

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