Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在西纳特拉混合路线,以获得更好的结构_Ruby_Sinatra - Fatal编程技术网

Ruby 如何在西纳特拉混合路线,以获得更好的结构

Ruby 如何在西纳特拉混合路线,以获得更好的结构,ruby,sinatra,Ruby,Sinatra,我没有发现如何混合来自其他模块的路由,例如: module otherRoutes get "/route1" do end end class Server < Sinatra::Base include otherRoutes get "/" do #do something end end 模块其他路径 获取“/route1”do 结束 结束 类服务器

我没有发现如何混合来自其他模块的路由,例如:

module otherRoutes
  get "/route1" do

  end
end    

class Server < Sinatra::Base
  include otherRoutes

  get "/" do
    #do something
  end
end
模块其他路径
获取“/route1”do
结束
结束
类服务器
这可能吗?

您可以这样做:

module OtherRoutes
  def self.included( app )
    app.get "/route1" do
      ...
    end
  end
end

class Server < Sinatra::Base
  include OtherRoutes
  ...
end
模块其他路径
包括def自身(应用程序)
app.get“/route1”do
...
结束
结束
结束
类服务器

与Ramaze不同,Sinatra的路由不是方法,因此不能直接使用Ruby的方法查找链接。请注意,有了它,您以后就无法修补其他路由并将更改反映到服务器中;这只是定义路由的一次性便利。

您也可以使用map方法将路由映射到您的sinatra应用程序

map "/" do
  run Rack::Directory.new("./public")
end

map '/posts' do
  run PostsApp.new
end

map '/comments' do
  run CommentsApp.new
end


map '/users' do
  run UserssApp.new
end
你不包括Sinatra。您可以将扩展名与寄存器一起使用

即,在单独的文件中构建模块:

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
class服务器
从文档中还不清楚这是非基本Sinatra应用程序的发展方向。希望它能帮助别人。

只要我的两分钱:

my_app.rb:

require 'sinatra/base'

class MyApp < Sinatra::Base
  set :root, File.expand_path('../', __FILE__)
  set :app_file, __FILE__
  disable :run

  files_to_require = [
    "#{root}/app/helpers/**/*.{rb}",
    "#{root}/app/routes/**/*.{rb}"
  ]

  files_to_require.each {|path| Dir.glob(path, &method(:require))}
  helpers App::Helpers
end
app/helpers/application.rb:

module App
  module Helpers
    def t(*args)
      ::I18n::t(*args)
    end

    def h(text)
      Rack::Utils.escape_html(text)
    end
  end
end
config.ru:

require './my_app.rb'

我更喜欢使用sinatra contrib gem来扩展sinatra,以获得更清晰的语法和共享的名称空间

  # Gemfile
  gem 'sinatra', '~> 1.4.7'
  gem 'sinatra-contrib', '~> 1.4.6', require: 'sinatra/extension'

  # other_routes.rb
  module Foo
    module OtherRoutes
      extend Sinatra::Extension
      get '/some-other-route' do
        'some other route'
      end
    end
  end

  # app.rb
  module Foo
    class BaseRoutes < Sinatra::Base
      get '/' do
        'base route'
      end

      register OtherRoutes
    end
  end
#文件
宝石“sinatra”,“~>1.4.7”
gem“sinatra contrib”“~>1.4.6”要求:“sinatra/扩展”
#其他_routes.rb
模块Foo
模块其他路径
扩展Sinatra::扩展
找到“/其他路线”做什么
“其他路线”
结束
结束
结束
#app.rb
模块Foo
类BaseRoutes

与sinatra项目一起维护

警告:这种方法的局限性在于map只接受原始字符串(不允许使用regexp)。这似乎是组织应用程序的一种很好的方法,但字符串的局限性仍然存在吗?很好。我在这里使用了您的示例:为什么需要注册两次?您只需要从模块化应用程序调用register,因为路由文件中的register只会添加到主命名空间。
  # Gemfile
  gem 'sinatra', '~> 1.4.7'
  gem 'sinatra-contrib', '~> 1.4.6', require: 'sinatra/extension'

  # other_routes.rb
  module Foo
    module OtherRoutes
      extend Sinatra::Extension
      get '/some-other-route' do
        'some other route'
      end
    end
  end

  # app.rb
  module Foo
    class BaseRoutes < Sinatra::Base
      get '/' do
        'base route'
      end

      register OtherRoutes
    end
  end