diff --git a/lib/phoenix/router.ex b/lib/phoenix/router.ex index 3408c24454..599acea1b2 100644 --- a/lib/phoenix/router.ex +++ b/lib/phoenix/router.ex @@ -337,9 +337,9 @@ defmodule Phoenix.Router do {matches, _} = Enum.map_reduce(routes_with_exprs, %{}, &build_match/2) checks = - for {%{line: line}, %{dispatch: {plug, params}}} <- routes_with_exprs, into: %{} do + for {%{line: line, plug: plug, plug_opts: plug_opts}, _} <- routes_with_exprs, into: %{} do quote line: line do - {unquote(plug).init(unquote(params)), true} + {unquote(plug).init(unquote(Macro.escape(plug_opts))), []} end end @@ -823,12 +823,9 @@ defmodule Phoenix.Router do The router pipelines will be invoked prior to forwarding the connection. - The forwarded plug will be initialized at compile time. - - Note, however, that we don't advise forwarding to another - endpoint. The reason is that plugs defined by your app - and the forwarded endpoint would be invoked twice, which - may lead to errors. + However, we don't advise forwarding to another endpoint. + The reason is that plugs defined by your app and the forwarded + endpoint would be invoked twice, which may lead to errors. ## Examples diff --git a/test/phoenix/router/forward_test.exs b/test/phoenix/router/forward_test.exs index e60a5df73a..2bca5dcafc 100644 --- a/test/phoenix/router/forward_test.exs +++ b/test/phoenix/router/forward_test.exs @@ -14,6 +14,7 @@ defmodule Phoenix.Router.ForwardTest do def index(conn, _params), do: text(conn, "admin index") def stats(conn, _params), do: text(conn, "stats") + def params(conn, params), do: text(conn, inspect(params)) def api_users(conn, _params), do: text(conn, "api users") def api_root(conn, _params), do: text(conn, "api root") defp assign_fwd_script(conn, _), do: assign(conn, :fwd_script, conn.script_name) @@ -56,8 +57,9 @@ defmodule Phoenix.Router.ForwardTest do forward "/admin", AdminDashboard forward "/init", InitPlug forward "/assign/opts", AssignOptsPlug, %{foo: "bar"} - scope "/internal" do - forward "/api/v1", ApiRouter + + scope "/params/:param" do + forward "/", Controller, :params end end end @@ -114,9 +116,9 @@ defmodule Phoenix.Router.ForwardTest do conn = call(Router, :get, "admin") assert conn.private[Router] == {[], %{ Phoenix.Router.ForwardTest.AdminDashboard => ["admin"], - Phoenix.Router.ForwardTest.ApiRouter => ["api", "v1"], Phoenix.Router.ForwardTest.InitPlug => ["init"], - Phoenix.Router.ForwardTest.AssignOptsPlug => ["assign", "opts"] + Phoenix.Router.ForwardTest.AssignOptsPlug => ["assign", "opts"], + Phoenix.Router.ForwardTest.Controller => [] }} assert conn.private[AdminDashboard] == {["admin"], %{Phoenix.Router.ForwardTest.ApiRouter => ["api-admin"]}} @@ -148,6 +150,10 @@ defmodule Phoenix.Router.ForwardTest do assert call(Router, :get, "assign/opts").assigns.opts == %{foo: "bar"} end + test "forward can handle params" do + assert call(Router, :get, "params/hello_world").resp_body =~ ~s["param" => "hello_world"] + end + test "forward with scoped alias" do conn = call(ApiRouter, :get, "health") assert conn.resp_body == "health"