Skip to content

Commit 8c80e09

Browse files
authored
Add Phoenix.Controller.assign/2 to match LiveView API (#6513)
Adds `assign/2` to Phoenix.Controller for consistency with the LiveView API. This delegates to `Plug.Conn.merge_assigns/2` and provides a familiar interface for developers moving between controllers and LiveViews. Before: ``` conn |> assign(:name, "Alice") |> assign(:role, :admin) ``` After: ``` assign(conn, name: "Alice", role: :admin) ``` This mirrors the `assign/2` function available in LiveView, making the assign API consistent across both controller and LiveView contexts
1 parent 7e47929 commit 8c80e09

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/phoenix/controller.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,20 @@ defmodule Phoenix.Controller do
18891889
Phoenix.VerifiedRoutes.unverified_url(conn, current_path(conn, params))
18901890
end
18911891

1892+
@doc """
1893+
Assigns multiple key-value pairs to the connection.
1894+
1895+
This function accepts a map or keyword list of assigns and merges them into
1896+
the connection's assigns. It is equivalent to calling `Plug.Conn.assign/3`
1897+
multiple times.
1898+
1899+
## Examples
1900+
1901+
assign(conn, name: "Alice", role: :admin)
1902+
assign(conn, %{name: "Alice", role: :admin})
1903+
"""
1904+
defdelegate assign(conn, assigns), to: Plug.Conn, as: :merge_assigns
1905+
18921906
@doc false
18931907
def __plugs__(controller_module, opts) do
18941908
if Keyword.get(opts, :put_default_views, true) do

0 commit comments

Comments
 (0)