Post 在 Phoenix 中有参数(没有 Ecto)?

Post with params in Phoenix (with no Ecto)?

我正在尝试创建一个没有 Ecto 或早午餐的 REST API using Phoenix

在 router/controller 中创建带有参数但不使用 Ecto 的 post 函数的语法是什么?

例如在 Ruby/Sinatra 中它看起来像这样:

post "/v1/ipf" do
  @weight1 = params[:weight1]
  @weight2 = params[:weight2]
  @weight3 = params[:weight3]
  @weight4 = params[:weight4]
  @goal1_percent = params[:goal1_percent]
  @goal2_percent = params[:goal2_percent]

  # etc...
end

更新

根据 Nick 的回答,我得到的结果如下:

rest_api/web/router.ex:

defmodule RestApi.Router do
  use RestApi.Web, :router

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", RestApi do
    pipe_through :api

    scope "/v1", V1, as: :v1 do
      get "/ipf", IPFController, :ipf
    end
  end

end

rest_api/web/controllers/v1/ipf_controller.ex:

defmodule RestApi.V1.IPFController do
  use RestApi.Web, :controller

  import IPF

  def ipf(conn, params) do
    {weight1, _} = Integer.parse(params["weight1"])
    {weight2, _} = Integer.parse(params["weight2"])
    {weight3, _} = Integer.parse(params["weight3"])
    {weight4, _} = Integer.parse(params["weight4"])
    {goal1_percent, _} = Float.parse(params["goal1_percent"])
    {goal2_percent, _} = Float.parse(params["goal2_percent"])

    results = IPF.ipf(weight1, weight2, weight3, weight4, goal1_percent, goal2_percent)

    render conn, results: results
  end

end

rest_api/web/views/v1/ipf_view.ex:

defmodule RestApi.V1.IPFView do
  use RestApi.Web, :view

  def render("ipf.json", %{results: results}) do
    results
  end

end

Ecto 和 Brunch 真的没有什么可做的 w/Phoenix 处理 POST。 Brunch是一个web资产构建工具,Ecto是一个数据库层。

要添加这条新路由,您只需要在路由器中为新路由添加一个条目:

post "/v1/spf", SPFController, :spf

然后创建控制器:

defmodule MyModule.SPFController do
  def spf(conn, params) do
    # do whatever
  end
end

就是这样。