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)设置一个插头。
我将如何使用当前的工具来解决这个问题?
正如文档中所述 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)
另一种解决方案是直接在控制器中使用插头
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)