Phoenix-具有多个渲染的控制器 [英] Phoenix - controller with multiple render

查看:94
本文介绍了Phoenix-具有多个渲染的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Elixir + Phoenix创建应用程序,该应用程序将能够处理浏览器"和"api"请求以处理其资源.

Trying to create an app with Elixir + Phoenix, that would be able to handle both "browser" and "api" requests to handle its resources.

是否可以执行此操作而不必执行类似操作:

Is it possible to do it without having to do something like that :

scope "/", App do
  pipe_through :browser

  resources "/users", UserController
end

scope "/api", App.API as: :api do
  pipe_through :api

  resources "/users", UserController
end

这意味着必须创建两个控制器,它们可能具有相同的行为,不同之处在于它将使用 browser 管道和 api 的JSON呈现HTML. >管道.

which would mean having to create two controllers, which might have the same behavior, except that it will render HTML with the browser pipeline and, say JSON, for the api pipeline.

我在想也许是Rails respond_to do |format| ...

I was thinking maybe something like the Rails respond_to do |format| ...

推荐答案

我不建议这样做(我建议有两个控制器并将您的逻辑移到两个控制器都调用的不同模块中),但是可以做到.您可以共享一个控制器,但是仍然需要一个单独的管道来确保设置了正确的响应类型(html/json).

I wouldn't recommend it (I would recommend having two controllers and move your logic into a different module that is called by both controllers) but it can be done. You can share a controller, but you still need a separate pipeline to ensure the correct response type (html/json) is set.

以下将使用相同的控制器和视图,但根据路线呈现json或html. "/"是html,"/api"是json.

The following will use the same controller and view, but render json or html depending on the route. "/" is html, "/api" is 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

如果您使用类似这样的路由器,您还可以共享路由器并使用同一路由来提供所有服务:

You can also share the router and have everything served by the same route if you use a router like:

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

然后您可以在网址末尾使用?format=json指定格式-我建议为您的API和网站使用不同的网址.

You can then specify the format using ?format=json at the end of the url - I would recommend going with different urls for your API and site however.

这篇关于Phoenix-具有多个渲染的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆