How mix in routes in Sinatra for a better structure

前端 未结 5 875
庸人自扰
庸人自扰 2020-12-13 07:28

I found nothing about how I can mix-in routes from another module, like this:

module otherRoutes
  get \"/route1\" do

  end
end    

class Server < Sinat         


        
5条回答
  •  一整个雨季
    2020-12-13 07:49

    I prefer the use of sinatra-contrib gem to extend sinatra for cleaner syntax and shared namespace

      # 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
    

    sinata-contrib is maintained alongside the sinatra project

提交回复
热议问题