Chen Yu
Chen Yu

Reputation: 4077

where is the definition of `pipeline`,`scope` in router.ex in phoenix project?

I am reading phoenix project through vscode. What's the meaning of pipeline,scope in the router.ex file.

It can't be found reference by vscode. It seams that they are not keywords of elixir language. What's are they?

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, {HelloWeb.LayoutView, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug HelloWeb.Plugs.Locale, "en"
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", HelloWeb do
    pipe_through :browser

    get "/", PageController, :index
    get "/hello", HelloController, :index
    get "/hello/:messenger", HelloController, :show
  end

Upvotes: 0

Views: 193

Answers (1)

Everett
Everett

Reputation: 9578

They are macros: during compilation they will evaluate and create regular code (i.e. macros are "code that creates code"). Macros can help you define simple interfaces and quickly generate lots of tedious code, but they are definitely more difficult to debug and follow in an editor.

In this case, these macros are handy for defining a domain specific language (DSL) that offers an expressive way to define routes and middleware.

Macros can be defined in any of your code or in your dependencies. Look for the defmacro keyword. In this case, they're defined within the Phoenix Router module.

Upvotes: 1

Related Questions