   end
 
   defp add_route(verb, path, controller, action, options) do
-    quote bind_quoted: binding() do
-      route = Scope.route(__MODULE__, verb, path, controller, action, options)
-      exprs = Route.exprs(route)
-
-      @phoenix_routes {route, exprs}
-
-      defp match(var!(conn), unquote(route.verb), unquote(exprs.path), unquote(exprs.host)) do
-        unquote(exprs.dispatch)
+    route =
+      quote do
+        route = Scope.route(__MODULE__, unquote(verb), unquote(path), unquote(controller),
+                                        unquote(action), unquote(options))
+        exprs = Route.exprs(route)
+        @phoenix_routes {route, exprs}
+      end
+
+    definition =
+      quote unquote: false do
+        defp match(var!(conn), unquote(route.verb), unquote(exprs.path), unquote(exprs.host)) do
+          unquote(exprs.dispatch)
+        end
+      end
+
+    quote do
+      try do
+        unquote(route)
+        unquote(definition)
+      after
+        :ok
       end
     end
   end

    add_socket(mount, Dict.merge(opts, alias: chan_alias), chan_block)
  end
  defp add_socket(mount, opts, chan_block) do
    quote bind_quoted: [mount: mount, opts: opts], unquote: true do
      if Scope.inside_scope?(__MODULE__) do
        raise """
        You are trying to call `socket` within a `scope` definition.
        Please move your socket and channel definitions outside of any scope block.
        """
      end

      @phoenix_socket_mount mount
      @phoenix_transports opts[:via]
      @phoenix_channel_alias opts[:alias]
      get  @phoenix_socket_mount, Phoenix.Transports.WebSocket, :upgrade, Dict.take(opts, [:as])
      post @phoenix_socket_mount, Phoenix.Transports.WebSocket, :upgrade
      get  @phoenix_socket_mount <> "/poll", Phoenix.Transports.LongPoller, :poll
      post @phoenix_socket_mount <> "/poll", Phoenix.Transports.LongPoller, :publish
      unquote(chan_block)
      @phoenix_socket_mount nil
      @phoenix_transports nil
      @phoenix_channel_alias nil
    end
  end


  defmacro pipeline(plug, do: block) do
    block =
      quote do
        plug = unquote(plug)
        @phoenix_pipeline []
        unquote(block)
      end

    compiler =
      quote unquote: false do
        Scope.pipeline(__MODULE__, plug)
        {conn, body} = Plug.Builder.compile(@phoenix_pipeline)
        defp unquote(plug)(unquote(conn), _), do: unquote(body)
        @phoenix_pipeline nil
      end

    quote do
      try do
        unquote(block)
        unquote(compiler)
      after
        :ok
      end
    end
  end

  @doc """
