gpt4 book ai didi

elixir - 如何在phoenix框架中的另一个管道定义中重用路由器管道定义?

转载 作者:行者123 更新时间:2023-12-02 00:10:48 24 4
gpt4 key购买 nike

我需要在 web/router.ex 文件中定义两个管道,如下所示:

pipeline :api do
plug :accepts, ["json"]
plug :fetch_session
plug MyApp.Plugs.ValidatePayload
end

pipeline :restricted_api do
plug :accepts, ["json"]
plug :fetch_session
plug MyApp.Plugs.ValidatePayload
plug MyApp.Plugs.EnsureAuthenticated
plug MyApp.Plugs.EnsureAuthorized
end

您可以清楚地看到 :api 管道中的步骤在 :restricted_api 管道中重复。

是否可以在 :restricted_api 管道中重用 :api 管道?

我正在考虑类似于继承的事情:

pipeline :api do
plug :accepts, ["json"]
plug :fetch_session
plug MyApp.Plugs.ValidatePayload
end

pipeline :restricted_api do
extend :api
plug MyApp.Plugs.EnsureAuthenticated
plug MyApp.Plugs.EnsureAuthorized
end

最佳答案

pipeline 宏创建一个函数插件。因此,它可以像任何其他具有 plug :pipeline 的插头一样用于其他管道。在提供的示例中:

pipeline :api do
plug :accepts, ["json"]
plug :fetch_session
plug MyApp.Plugs.ValidatePayload
end

pipeline :restricted_api do
plug :api
plug MyApp.Plugs.EnsureAuthenticated
plug MyApp.Plugs.EnsureAuthorized
end

关于elixir - 如何在phoenix框架中的另一个管道定义中重用路由器管道定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44984581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com