Phoenix 1.4 How to alias Routes.page_path in a Plug

1.4k Views Asked by At

I've tried a couple different iterations but I keep getting this error when compiling:

Routes.session_path/2 is undefined (module Routes is not available)

My Code:

defmodule Blackbook.Plugs.RequireAuth do
    import Plug.Conn
    import Phoenix.Controller
    alias Blackbook.Router.Helpers, as: Routes

    def init(_params) do
    end

    def call(conn, _params) do
        if conn.assigns[:current_user] do
            conn
        else
            conn
            |> put_flash(:error, "You must be logged in.")
            |> redirect(to: Routes.session_path(conn, :new))
            |> halt()
        end
    end
end

It's a little different in Phoenix 1.4, tried referring to documentation here https://hexdocs.pm/phoenix/Phoenix.Router.html but still no luck.

1

There are 1 best solutions below

0
On BEST ANSWER

Since you're using , it creates a separate YourAppWeb namespace for web-related modules by default. In your case, I believe it's just a typo and it should be this:

alias BlackbookWeb.Router.Helpers, as: Routes

(Note the Web part)