IlikedCPlusPlus
IlikedCPlusPlus

Reputation: 57

Phoenix framework - :options callback

I'm currently developing a backend server which is based on the Phoenix framework. However, I don't understand one thing: I can define specific routes which are called whenever I use a certain HTTP verb to access said route. Since I couldn't perform any pre-flight request, I also added a OPTIONS route for each file which leads to the following scope block inside my router.ex file:

post "/someroute", SomeController, :some_handler
options "/someroute, SomeController, :options

All of this code is inside the router.ex file which is contained in the lib/_web folder and defines the routes of the backend as well as their respective handlers by using a DSL. While I know that some_handler is a function which is contained in some controller, I unfortunately have no idea where the function :options (or its atom) is defined. Does anyone have an idea where I can find something like the implementation of :options?

Upvotes: 0

Views: 71

Answers (1)

Everett
Everett

Reputation: 9558

In this case, options is a macro (just like get or post). Macros have the benefit of allowing you to write out DSL's in a more readable way, but the drawback is that they are far more opaque than normal functional code because they generate the code. Look inside deps/phoenix/lib/phoenix/router.ex and you'll see that this isn't even a simple macro, it's looped:

  for verb <- @http_methods do
    @doc """
    Generates a route to handle a #{verb} request to the given path.

        #{verb}("/events/:id", EventController, :action)

    See `match/5` for options.
    """
    defmacro unquote(verb)(path, plug, plug_opts, options \\ []) do
      add_route(:match, unquote(verb), path, plug, plug_opts, options)
    end
  end

Upvotes: 1

Related Questions