如果满足“ some_condition”,我想将用户重定向到某个页面。例如,如果根本没有创建任何文章,则用户必须创建一个文章。重定向必须从所有控制器及其中的动作发生。
我有一个插头:
defmodule P1 do
alias Plug.Conn
def init(opts), do: opts
def call(conn, opts) do
if some_condition do
Phoenix.Controller.redirect(conn, to: MyApp.Router.Helpers.article_path(conn, :new))
|> Plug.Conn.halt()
end
end
end
与此有关的一个问题是
a)它将所有请求重定向到“ article_path / new”,包括js,css等文件。也就是说,每条路线会连续多次出现。而不是我想要的一个重定向。
b)无法呈现页面。当我以这种方式重定向用户时,它将显示为空白页面
该如何解决?
答案 0 :(得分:1)
您应该为不同类型的内容定义different pipelines,并仅在其所属的地方包含此插件。
有点像
pipeline :authenticate do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :p1 # yours one
end
scope "/app", AppWeb do
pipe_through :authenticate
get "/", ...
...
end