Elixir 长生不老药凤凰插头和放置位置

Elixir 长生不老药凤凰插头和放置位置,elixir,phoenix-framework,Elixir,Phoenix Framework,我目前正在学习长生不老药凤凰框架。我现在陷入困境,是关于如何使用forward命令 这里写着,现在不需要学习Plug,所以如果我想尝试forward命令,我可以将Plug的实现复制到我的应用程序中。但具体在哪里?我试着去寻找它,试着使用我发现的东西,但我做错了什么 这是我的\lib\hello\u web\router.ex文件中的范围: scope "/", HelloWeb do pipe_through :browser # Use the default browser stack

我目前正在学习长生不老药凤凰框架。我现在陷入困境,是关于如何使用
forward
命令

这里写着,现在不需要学习Plug,所以如果我想尝试
forward
命令,我可以将Plug的实现复制到我的应用程序中。但具体在哪里?我试着去寻找它,试着使用我发现的东西,但我做错了什么

这是我的
\lib\hello\u web\router.ex
文件中的范围:

scope "/", HelloWeb do
  pipe_through :browser # Use the default browser stack

  get "/", PageController, :index
  forward "/jobs", BackgroundJob.Plug, name: "Hello, Phoenix"
end
这就是插头的代码:

defmodule BackgroundJob.Plug do
  def init(opts), do: opts
  def call(conn, opts) do
    conn
    |> Plug.Conn.assign(:name, Keyword.get(opts, :name, "Background Job"))
    |> BackgroundJob.Router.call(opts)
  end
end

defmodule BackgroundJob.Router do
  use Plug.Router

  plug :match
  plug :dispatch

  get "/", do: send_resp(conn, 200, "Welcome to #{conn.assigns.name}")
  get "/active", do: send_resp(conn, 200, "5 Active Jobs")
  get "/pending", do: send_resp(conn, 200, "3 Pending Jobs")
  match _, do: send_resp(conn, 404, "Not found")
end

在这一点上,我对插头和凤凰几乎一无所知,所以这可能是显而易见的,但我真的在那里呆了几个小时。有人能帮我找到代码的位置吗?

欢迎来到stackoverflow

您还没有发布您遇到的错误,但我猜您的问题是模块命名。在
phoenix
中,在
作用域
块中使用的模块将以您作为该作用域参数给出的模块名称作为前缀(
HelloWeb
)。因此,将插件模块的名称更改为
HelloWeb.BackgroundJob.plug
就足够了。或者,您不能将作用域模块传递给
范围

scope "/" do
  pipe_through :browser # Use the default browser stack

  get "/", HelloWeb.PageController, :index
  forward "/jobs", BackgroundJob.Plug, name: "Hello, Phoenix"
end
注意本例中的更改
PageController
->
HelloWeb.PageController


就放置位置而言,
lib
中的任何
.ex
文件都可以,因为elixir只需编译所有文件。事实上,您只需将代码粘贴到
路由器.ex
中即可快速测试。按照惯例,文件名应该跟在模块名后面,因此
HelloWeb.BackgroundJob.Plug
应该位于
lib/hello\u web/background\u job/Plug.ex
是的,我理解你的困惑。 在指南中可以解释得更清楚一点

如果将模块名传递给作用域,则传入的模块名将作为作用域中所有模块的前缀

例如,假设传入的模块名是
HelloWeb
, 当您将其传递到如下范围时:

scope "/", HelloWeb do
  ...
  get "/", PageController, :index
  forward "/jobs", BackgroundJob.Plug
end
helloweb.backgroundjob.plug.init/1 is undefined
scope "/",  do
  ...
  get "/", HelloWeb.PageController, :index
  forward "/jobs", BackgroundJob.Plug
end
  scope "/", HelloWeb do
    ...
    get "/", PageController, :index
  end

  scope "/jobs" do
    forward "/", BackgroundJob.Plug, name: "Hello Phoenix"
  end
“/”
路由将查找
HelloWeb.PageController

“/jobs”
路由将查找
HelloWeb.BackgroundJob.Plug

为了消除你的错误,我假设是这样的:

scope "/", HelloWeb do
  ...
  get "/", PageController, :index
  forward "/jobs", BackgroundJob.Plug
end
helloweb.backgroundjob.plug.init/1 is undefined
scope "/",  do
  ...
  get "/", HelloWeb.PageController, :index
  forward "/jobs", BackgroundJob.Plug
end
  scope "/", HelloWeb do
    ...
    get "/", PageController, :index
  end

  scope "/jobs" do
    forward "/", BackgroundJob.Plug, name: "Hello Phoenix"
  end
您可以从作用域中删除传入的模块名称,并手动预加模块名称,保留
BackgroundJob.Plug
不变,如下所示:

scope "/", HelloWeb do
  ...
  get "/", PageController, :index
  forward "/jobs", BackgroundJob.Plug
end
helloweb.backgroundjob.plug.init/1 is undefined
scope "/",  do
  ...
  get "/", HelloWeb.PageController, :index
  forward "/jobs", BackgroundJob.Plug
end
  scope "/", HelloWeb do
    ...
    get "/", PageController, :index
  end

  scope "/jobs" do
    forward "/", BackgroundJob.Plug, name: "Hello Phoenix"
  end
或者,可以省去一些重复的键入,而是添加一个“/jobs”范围,如下所示:

scope "/", HelloWeb do
  ...
  get "/", PageController, :index
  forward "/jobs", BackgroundJob.Plug
end
helloweb.backgroundjob.plug.init/1 is undefined
scope "/",  do
  ...
  get "/", HelloWeb.PageController, :index
  forward "/jobs", BackgroundJob.Plug
end
  scope "/", HelloWeb do
    ...
    get "/", PageController, :index
  end

  scope "/jobs" do
    forward "/", BackgroundJob.Plug, name: "Hello Phoenix"
  end
请注意,示例中的
代表了
管道通过
方法和其他路线,这些方法和路线对于传达想法来说不是必不可少的


希望有帮助

非常感谢,现在可以用了!但是我认为在这里澄清一下,对于将来遇到同样问题的人来说是很有用的,在
router.ex
范围
背景作业中。Plug
不会变成
HelloWeb.BackgroundJob.Plug
,否则它将导致
(未定义的功能错误)函数BackgroundJob.Router.call/2未定义(模块BackgroundJob.Router不可用)
错误。