Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 如何跳过特定页面的插件_Elixir_Phoenix Framework - Fatal编程技术网

Elixir 如何跳过特定页面的插件

Elixir 如何跳过特定页面的插件,elixir,phoenix-framework,Elixir,Phoenix Framework,我正在构建一个具有身份验证的Phoenix应用程序。在我的路由器中,我有如下功能: pipeline :browser do plug :accepts, ["html"] plug MyApp.Plugs.Authenticate end scope "/", MyApp do pipe_through :browser # Use the default browser stack get "/", HomeController, :show ge

我正在构建一个具有身份验证的Phoenix应用程序。在我的路由器中,我有如下功能:

pipeline :browser do
    plug :accepts, ["html"]
    plug MyApp.Plugs.Authenticate
end

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

    get "/", HomeController, :show
    get "/login", SessionsController, :login
    get "/matches", MatchesController, :index
end
我想跳过/login的身份验证插件,我可以在路由器中执行此操作,还是必须在插件本身中执行此操作

插头。验证看起来像:

def call(conn, _) do
    case Authenticator.find_user(conn) do
        {:ok, user} ->
            assign(conn, :user, user)
        :error ->
            conn
                |> redirect(to: "/login")
                |> halt
    end
end

一种方法是定义一个单独的管道:

pipeline :browser do
    plug :accepts, ["html"]
end

pipeline :auth do
    plug MyApp.Plugs.Authenticate
end

scope "/", MyApp do
    pipe_through [:browser, :auth]

    get "/", HomeController, :show
    get "/matches", MatchesController, :index
end

scope "/", MyApp do
    pipe_through :browser

    get "/login", SessionsController, :login
end
这里有几件事需要注意

1) 在需要身份验证的示例中,管道被链接起来

2) 只要实际路由不同,就可以多次使用同一范围这是因为上面的路由大致编译为:

defmodule MyRouter do
  def match(conn, :get,    ["/"])
  def match(conn, :get,    ["/matches"])
  def match(conn, :get,    ["/login"])
end
您可以阅读更多有关Phoenix routing中宏如何工作的信息,请参阅