跳至内容 跳至搜索

MiddlewareStackProxy 是 Rails 中间件栈的代理,允许您在应用程序中配置中间件。它基本上充当命令记录器,将每个命令保存到初始化后应用于默认中间件栈,因此您可以添加、交换或删除 Rails 中的任何中间件。

您可以使用 config.middleware.use 方法添加自己的中间件

config.middleware.use Magical::Unicorns

这将把 Magical::Unicorns 中间件放在栈的末尾。如果您希望在另一个中间件之前添加一个中间件,可以使用 insert_before

config.middleware.insert_before Rack::Head, Magical::Unicorns

还有 insert_after,它将在另一个中间件之后插入一个中间件

config.middleware.insert_after Rack::Head, Magical::Unicorns

中间件也可以完全被交换掉并被其他中间件替换

config.middleware.swap ActionDispatch::Flash, Magical::Unicorns

中间件可以从一个地方移动到另一个地方

config.middleware.move_before ActionDispatch::Flash, Magical::Unicorns

这将把 Magical::Unicorns 中间件移动到 ActionDispatch::Flash 之前。您也可以将其移动到之后

config.middleware.move_after ActionDispatch::Flash, Magical::Unicorns

最后,它们也可以完全从栈中移除

config.middleware.delete ActionDispatch::Flash
方法
D
I
M
N
S
U

属性

[R] delete_operations
[R] operations

类公共方法

new(operations = [], delete_operations = [])

# File railties/lib/rails/configuration.rb, line 47
def initialize(operations = [], delete_operations = [])
  @operations = operations
  @delete_operations = delete_operations
end

实例公共方法

delete(...)

# File railties/lib/rails/configuration.rb, line 70
def delete(...)
  @delete_operations << -> middleware { middleware.delete(...) }
end

insert(...)

别名:insert_before

insert_after(...)

# File railties/lib/rails/configuration.rb, line 58
def insert_after(...)
  @operations << -> middleware { middleware.insert_after(...) }
end

insert_before(...)

也作为别名:insert
# File railties/lib/rails/configuration.rb, line 52
def insert_before(...)
  @operations << -> middleware { middleware.insert_before(...) }
end

move(...)

别名:move_before

move_after(...)

# File railties/lib/rails/configuration.rb, line 80
def move_after(...)
  @delete_operations << -> middleware { middleware.move_after(...) }
end

move_before(...)

也作为别名:move
# File railties/lib/rails/configuration.rb, line 74
def move_before(...)
  @delete_operations << -> middleware { middleware.move_before(...) }
end

swap(...)

# File railties/lib/rails/configuration.rb, line 62
def swap(...)
  @operations << -> middleware { middleware.swap(...) }
end

unshift(...)

# File railties/lib/rails/configuration.rb, line 84
def unshift(...)
  @operations << -> middleware { middleware.unshift(...) }
end

use(...)

# File railties/lib/rails/configuration.rb, line 66
def use(...)
  @operations << -> middleware { middleware.use(...) }
end