Is there a way to match controller action in conn struct?

In the docs I’ve already seen that Phoenix.Controller.action_name/1 returns the action name as an atom and raises an error if unavailable.

I also know that inside a controller I can do something like:

plug :my_plug when action in [:index]

But I would rather like to match it as below because I will be calling the plug inside my router:

  defp my_plug(conn = %{private: %{:phoenix_action => :index}}, _),
    do: put_flash(conn, :info, ":index action matched !!!!")

  defp my_plug(conn, _), do: put_flash(conn, :info, "Matching failed !!!!")

I’m doing that because after inspecting conn in the console I saw something like
%{..., private: %{..., :phoenix_action => :index, ....}, ...}.

Unfortunately the matching always fails.

Are there other ways to do this?

The value will be set as part of the controller plug pipeline, so the value won’t be set in the router. And even the controller doesn’t “query” the action from somewhere, but it’s literally called like MyApp.SomeController.call(conn, action) by the router. So only the matching route is aware of which action on the controller is meant to be called.

1 Like

Thank you, I just understood better what is going on.
After inspecting conn inside the plug itself when called from the router, the :phoenix_action => :index part of the inspect output is no more available. That is to set later…