有没有办法让 Phoenix Plug 只用于一条路线?

Is there a way to have a Phoenix Plug just for one route?

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

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

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

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

正如 Phoenix.Router.pipeline/2

的文档中所述

Every time pipe_through/1 is called, the new pipelines are appended to the ones previously given.

也就是说,这会起作用:

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

  pipe_through :post_plug
  post "/register", RegistrationController, :register
end

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

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

  def register(conn, params) do
    # ...
  end
end