Skip to content

Commit f1b34fc

Browse files
authored
Add --method option to phx.routes (phoenixframework#5525)
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.
1 parent e4580ff commit f1b34fc

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/mix/tasks/phx.routes.ex

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ defmodule Mix.Tasks.Phx.Routes do
3434
## Options
3535
3636
* `--info` - locate the controller function definition called by the given url
37+
* `--method` - what HTTP method to use with the given url, only works when used with `--info` and defaults to `get`
3738
3839
## Examples
3940
@@ -51,6 +52,13 @@ defmodule Mix.Tasks.Phx.Routes do
5152
Module: RouteInfoTestWeb.PageController
5253
Function: :index
5354
/home/my_app/controllers/page_controller.ex:4
55+
56+
Print information about the controller function called by a specified url and HTTP method:
57+
58+
$ mix phx.routes --info http://0.0.0.0:4000/users --method post
59+
Module: RouteInfoTestWeb.UserController
60+
Function: :create
61+
/home/my_app/controllers/user_controller.ex:24
5462
"""
5563

5664
@doc false
@@ -78,10 +86,11 @@ defmodule Mix.Tasks.Phx.Routes do
7886
end
7987
end
8088

81-
def get_url_info(url, {router_mod, _opts}) do
89+
def get_url_info(url, {router_mod, opts}) do
8290
%{path: path} = URI.parse(url)
8391

84-
meta = Phoenix.Router.route_info(router_mod, "GET", path, "")
92+
method = opts |> Keyword.get(:method, "get") |> String.upcase()
93+
meta = Phoenix.Router.route_info(router_mod, method, path, "")
8594
%{plug: plug, plug_opts: plug_opts} = meta
8695

8796
{module, func_name} =

0 commit comments

Comments
 (0)