23

I'm creating my first Elixir-Phoenix app. I've written a few plugs that I want to use in multiple controllers, right now there is a lot of code duplication since the Plug code is being repeated in all of my controllers.

My question is, is there a file where I can put all of my Plugs and then access and use them from different controllers?

# somefile to define my plugs

defp load_subject(conn, _) do
    subject = Subject |> Repo.get conn.params["subject_id"]

    assign(conn, :subject, subject)
end

defp load_topic(conn, _) do
    topic = Topic |> Repo.get conn.params["topic_id"]

    conn |> assign :topic, topic
end

Then use them in my controllers:

# First Controller
defmodule MyApp.FirstController do
    use MyApp.Web, :controller

    plug :load_subject
    plug :load_topic
    plug :action

    def some_action(conn, _) do
        # do something
    end

    def another_action(conn, _) do
        # do some other thing
    end
end

# Second Controller
defmodule MyApp.SecondController do
    use MyApp.Web, :controller

    plug :load_subject
    plug :load_topic
    plug :action

    def lame_action(conn, _) do
        # do something
    end

    def cool_action(conn, _) do
        # do some other thing
    end
end
4

1 回答 1

27

您可以将任何模块中的插件定义为公共函数:

defmodule MyApp.Loaders do
  import Plug.Conn

  def load_subject(conn, _) do
    subject = Subject |> Repo.get conn.params["subject_id"]
    assign(conn, :subject, subject)
  end

  def load_topic(conn, _) do
    topic = Topic |> Repo.get conn.params["topic_id"]
    conn |> assign :topic, topic
  end
end

现在您可以将其导入控制器并使用插件:

defmodule MyApp.Controller do
  use MyApp.Web, :controller
  import MyApp.Loaders

  plug :load_subject
  plug :load_topic
  plug :action

  ...
end

或者,您也可以在路由器中创建管道:

import MyApp.Loaders

pipeline :with_subject_and_topic do
  plug :load_subject
  plug :load_topic
end

然后pipe_through在相关范围内。

于 2015-06-21T07:52:51.930 回答