有没有办法让凤凰插头只用于一条路线?

tho*_*odg 3 routing elixir phoenix-framework plug

在凤凰城,我的路线如下:

  scope "/", ManaWeb do
    pipe_through [:browser, :auth]
    get "/register",  RegistrationController, :new
    post "/register", RegistrationController, :register
  end
Run Code Online (Sandbox Code Playgroud)

但是我想为最后一条路线(POST)设置一个插头。

我将如何使用当前的工具来解决这个问题?

Ale*_*kin 6

正如文档中所述 Phoenix.Router.pipeline/2

每次pipe_through/1调用时,新管道都会附加到先前给出的管道中。

也就是说,这会起作用:

scope "/", ManaWeb do
  pipe_through [:browser, :auth]
  get "/register",  RegistrationController, :new

  pipe_through :post_plug
  post "/register", RegistrationController, :register
end
Run Code Online (Sandbox Code Playgroud)

  • 请记住,您放置在 `pipe_through :post_plug` 下面的任何路由都将调用 `:post_plug`。 (2认同)

fhd*_*sni 5

另一种解决方案是直接在控制器中使用插头

defmodule ManaWeb.RegistrationController do
  # import the post_plug...
  plug :post_plug when action in [:register]

  def register(conn, params) do
    # ...
  end
end
Run Code Online (Sandbox Code Playgroud)