Skip to content

How to set a default route for /admin #723

Answered by Flo0807
aaronzomback asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @aaronzomback!

You can create a (redirect) controller for this use case.

In your router.ex file:

scope "/admin", MyAppWeb do
  get "/", RedirectController, :redirect_to_posts
end

For example, in my_app_web/controller create a file named redirect_controller.ex:

defmodule MyAppWeb.RedirectController do
  use MyAppWeb, :controller

  def redirect_to_posts(conn, _params) do
    conn
    |> Phoenix.Controller.redirect(to: ~p"/admin/posts")
    |> Plug.Conn.halt()
  end
end

That's it!

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@aaronzomback
Comment options

@Flo0807
Comment options

Answer selected by aaronzomback
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants