Beautiful Code

Elixir is perhaps the first functional language to support Lisp-style macros with a more natural syntax. This feature, like a template for code, is not always the right tool for everyday users, but macros are invaluable for extending the Elixir language to add the common features all web servers need to support.

For example, web servers need to map routes onto functions that do the job:

 pipeline :browser do
  plug :accepts, ["html"]
  plug :fetch_session
  plug :protect_from_forgery
 end
 
 pipeline :api do
  plug :accepts, ["json"]
 end
 
 scope "/", MyApp do
  pipe_through :browser
  get "/users", UserController, :index
  ...
 end
 
 scope "/api/", MyApp do
  pipe_through :api
  ...
 end

You’ll see this code a little later. ...

Get Programming Phoenix now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.