Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/phoenix/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,14 @@ defmodule Phoenix.Router do
end

defp build_pipes(name, pipe_through) do
plugs = pipe_through |> Enum.reverse() |> Enum.map(&{&1, [], true})
plugs =
pipe_through
|> Enum.reverse()
|> Enum.map(fn
{module, opts} -> {module, opts, true}
plug -> {plug, [], true}
end)

opts = [init_mode: Phoenix.plug_init_mode(), log_on_halt: :debug]
{conn, body} = Plug.Builder.compile(__ENV__, plugs, opts)

Expand Down Expand Up @@ -1036,6 +1043,11 @@ defmodule Phoenix.Router do

pipe_through [:require_authenticated_user, :my_browser_pipeline]

A module plug may also be given, optionally with a tuple of `{plug, opts}`
to pass options to it:

pipe_through [:browser, MyApp.Plugs.Locale, {MyApp.Plugs.Feature, flag: :beta}]

## Multiple invocations

`pipe_through/1` can be invoked multiple times within the same scope. Each
Expand Down
31 changes: 31 additions & 0 deletions test/phoenix/router/pipeline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,35 @@ defmodule Phoenix.Router.PipelineTest do
end
end
end

test "pipe_through with module plug and options" do
defmodule ModulePlugRouter do
use Phoenix.Router

defmodule GreetingPlug do
def init(opts), do: opts

def call(conn, opts) do
Plug.Conn.assign(conn, :greeting, Keyword.fetch!(opts, :greeting))
end
end

pipeline :api do
plug :put_assign, "api"
end

scope "/" do
pipe_through [:api, {GreetingPlug, greeting: "Hey there"}]
get "/hello", SampleController, :index
end

defp put_assign(conn, value) do
assign(conn, :stack, [value | conn.assigns[:stack] || []])
end
end

conn = call(ModulePlugRouter, :get, "/hello")
assert conn.assigns[:greeting] == "Hey there"
assert conn.assigns[:stack] == ["api"]
end
end