What does the scope keyword do?

What does the scope keyword do?

Example,

scope “/”, HelloWeb do
pipe_through :browser

get "/", PageController, :index: 

end

Scopes are a way to group routes under a common path prefix and scoped set of plug middleware. We might want to do this for admin functionality, APIs, and especially for versioned APIs. Let’s say we have user generated reviews on a site, and that those reviews first need to be approved by an admin. The semantics of these resources are quite different, and they might not share the same controller. Scopes enable us to segregate these routes.

from https://hexdocs.pm/phoenix/routing.html#scoped-routes

If i changed your example to

scope “/myscope”, HelloWeb do
pipe_through :browser

    get "/", PageController, :index: 
    get "/test", PageController, :test
end

My routes would be
/myscope/ -> PageController, :index
/myscope/test/ -> PageController, :test

2 Likes

Is this a standard Elixir keyword or a Phoenix function?

This is a function defined by Phoenix.

https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/2
https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/3
https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/4

3 Likes