What is the difference between forward and pipelines / scopes?

The “Routing” guide in the Phoenix docs says the following about the forward function:
“The Phoenix.Router.forward/4 macro can be used to send all requests that start with a particular path to a particular plug.”

However, from what I understood, scopes are used to do the same? So my question is, what would be the difference between:

scope "/jobs" do
  plug BackgroundJob.Plug
end

And:

forward "/jobs", BackgroundJob.Plug

Is it simply shorter? Or am I missing something? Thanks :grin:

I haven’t used it but it looks its to expose a plug/route provided by another app/library

Your first example does nothing, as there is no route that can match in your scope. scope also does allow other routes to match, for example:

scope "/a" do
  get "/foo", Foo.Controler
end

scope "/a" do
  get "/bar", Bar.Controller
end

This will define routes for /a/foo and /a/bar that can match.

On the other hand forward captures all request going for given prefix and will prevent any other request from matching that prefix. Aka

forward "/a", A.Plug

get "/a/foo", Foo.Controller

Foo.Controller will never be called when requesting /a/foo, as all requests prefixed with /a are captured by A.Plug.

6 Likes

Got it, thanks!