Skip to content

Commit

Permalink
Add --method option to phx.routes (phoenixframework#5525)
Browse files Browse the repository at this point in the history
Update the list of valid options in `Mix.Tasks.Phx.Routes`, adding a new
`--method` option, which, when combined with the `--info` option, allows
developers to specify what HTTP method should be used in conjunction
with the specified URL.
This should help in cases where the same URL matches multiple controller
actions, depending on the HTTP method. For example, an application with
a URL such as `/api/v1/users` might use it to list users, with the `GET`
method, or to create a new user, with the `POST` method.
  • Loading branch information
dinocosta authored Aug 2, 2023
1 parent e4580ff commit f1b34fc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/mix/tasks/phx.routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule Mix.Tasks.Phx.Routes do
## Options
* `--info` - locate the controller function definition called by the given url
* `--method` - what HTTP method to use with the given url, only works when used with `--info` and defaults to `get`
## Examples
Expand All @@ -51,6 +52,13 @@ defmodule Mix.Tasks.Phx.Routes do
Module: RouteInfoTestWeb.PageController
Function: :index
/home/my_app/controllers/page_controller.ex:4
Print information about the controller function called by a specified url and HTTP method:
$ mix phx.routes --info http://0.0.0.0:4000/users --method post
Module: RouteInfoTestWeb.UserController
Function: :create
/home/my_app/controllers/user_controller.ex:24
"""

@doc false
Expand Down Expand Up @@ -78,10 +86,11 @@ defmodule Mix.Tasks.Phx.Routes do
end
end

def get_url_info(url, {router_mod, _opts}) do
def get_url_info(url, {router_mod, opts}) do
%{path: path} = URI.parse(url)

meta = Phoenix.Router.route_info(router_mod, "GET", path, "")
method = opts |> Keyword.get(:method, "get") |> String.upcase()
meta = Phoenix.Router.route_info(router_mod, method, path, "")
%{plug: plug, plug_opts: plug_opts} = meta

{module, func_name} =
Expand Down

0 comments on commit f1b34fc

Please sign in to comment.