Where to Plug so that it is called for every action?

Where should one “plug” so that the function is called for every action of every controller BUT the controller/action is already known? Is there such a place? After the router but before the actual action.

Neither plugging into the router nor endpoint seems to be able to cut it…

You could place the plug at the top of the controller file, in each controller

1 Like

It is even possible to do it in a single line in my_app_web.ex.

def controller do
    quote do
      use Phoenix.Controller, namespace: MyAppWeb

      import Plug.Conn
      import MyAppWeb.Gettext
      alias MyAppWeb.Router.Helpers, as: Routes

      plug MyApp.MyPlug
1 Like

Or near the top of the router.ex file.

Isnt the router plugged in the Endpoint? :thinking:

Yes, sure. Just that’s not what I am looking for. I’d like to have it done once


? Where in the router would one have to put it so that both controller and action is already at play?