如何从 Sinatra 中删除路线?
我有一些动态加载的插件,它们在启动时注册它们的路由,但是我还需要能够在禁用它们时删除它们的路由。有没有办法删除现有的路线?
API 没有任何我能找到的方法来删除它们,我能想到的唯一其他方法就是直接访问 Sinatra::Base 中的 @routes 对象,但我不确定你是否可以对此采取任何措施,如果可以的话……这样做安全吗?
I have some dynamically loaded plugins which register their routes when they start up, however I also need to be able to remove their routes when they are disabled. Is there a way to remove existing routes?
The API didn't have any methods I could find to remove them, and the only other way I could think to do it would be to go right to the @routes object in Sinatra::Base, but I am not sure if you can do anything to that, and if you can... is it safe to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
浏览代码几分钟,我没有看到任何“破坏性”改变
路线
的代码,除了:这是一种“从轨道上摧毁它”的方法,并且可能不是您所需要的。根据这项调查,我认为您需要自己修改
routes
哈希值。对于当前版本的代码,这对我来说看起来“安全”,因为
route!
方法总是查找当前的路由数组并正常迭代它们(没有缓存):Sinatra 存储路由的内部结构最近发生了变化版本,所以我不会依赖它总是在不测试每个新版本的情况下工作。更好的是,提出一个补丁,看看是否可以将接受的功能合并到主库中。
Looking through the code for a few minutes I do not see any code that 'destructively' mutates the
routes
other than:This is a 'nuke it from orbit' approach, and presumably not what you need. Based on this investigation, I think you will need to modify the
routes
hash yourself.For the current version of code, this looks 'safe' to me, insofar as the
route!
method always looks up the current array of routes and iterates them normally (there is no caching):The internals of Sinatra around storing routes have shifted in recent releases, so I wouldn't rely on this always working without testing with each new release. Better yet, propose a patch and see if you can get the functionality accepted incorporated into the main library.
也许您考虑这种方法,
您可以将此
before
操作放在覆盖要删除的路由上,这样该路由就不会被访问和实现。如果您想再次启用该路由,只需删除
before
操作即可。或者删除数组disable_routes中的项目
May be you condider this approach,
You could put this
before
action for overriding the route you want to remove, so the route is not be accessed and implemented.If you want to enable that route again, just delete the
before
action.Or remove the item in array disable_routes
您可以通过设置 after 块来阻止路由返回任何内容。
You could prevent the route from returning anything by setting an after block.