私はそれをお勧めしません (2 つのコントローラーを用意し、両方のコントローラーによって呼び出される別のモジュールにロジックを移動することをお勧めします) が、それは可能です。コントローラーを共有できますが、正しい応答タイプ (html/json) が設定されていることを確認するには、別のパイプラインが必要です。
以下は同じコントローラーとビューを使用しますが、ルートに応じて json または html をレンダリングします。「/」はhtml、「/api」はjsonです。
ルーター:
defmodule ScopeExample.Router do
use ScopeExample.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", ScopeExample do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
end
scope "/api", ScopeExample do
pipe_through :api # Use the default browser stack
get "/", PageController, :index
end
end
コントローラ:
defmodule ScopeExample.PageController do
use ScopeExample.Web, :controller
plug :action
def index(conn, params) do
render conn, :index
end
end
意見:
defmodule ScopeExample.PageView do
use ScopeExample.Web, :view
def render("index.json", _opts) do
%{foo: "bar"}
end
end
次のようなルーターを使用する場合、ルーターを共有して、すべてを同じルートで処理することもできます。
defmodule ScopeExample.Router do
use ScopeExample.Web, :router
pipeline :browser do
plug :accepts, ["html", "json"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
end
scope "/", ScopeExample do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
end
end
その後、URL の最後に使用して形式を指定できます?format=json
。ただし、API とサイトには別の URL を使用することをお勧めします。