我不知道这是最好的主题名称我可以使用,但有没有在西纳特拉+机架的方式在PHP + apache的方式来使用类/模块怎么样?我的意思是有没有一种方法可以在请求范围内使用Singleton或任何其他静态类型变量?我需要(将要)是:西纳特拉请求范围,如PHP
class Config # might be just module
# some methods that are request scoped
# cant be shared between users/reuqest
def server_settings
# possible server dependent settings
# can be shared between requests on same server
end
end
class Something
def do_thing
# I would like to access Config class here without passing it as parameter
# setted up for the current request
end
end
class MyController < Sinatra::Base
# request handling, creates Config class, setting up request parameters once
def handle_it
Something.new.do_thing
end
end
这里没有轨道,我的事情我可以做/宝石我可以用非常有限的。 MyController来自内部框架。我可以扩展它。 Php + apache从没有任何请求,所以你的所有代码都是请求,除非你使用session/cookies。
我之所以想用这是为了避免将配置实例(或任何其他类型的数据存储库)到所有其他实例作为参数,只需访问“globaly”。
喜欢的东西[这](http://speakmy.name/2011/05/29/simple-因此我可以在'Something#do_thing'内调用'Appdata.my_data',它将被请求作用域(所以另一个请求不能看到第一个数据)。我不知道这对sinatra来说甚至是可能的,但会很棒。 – insider