script
December 4, 2018, 7:46am
1
In the phoenix routes we have paths for update/delete
like this:
/api/path/:id
Here :id
is a wildcard match. How can I mimic this behaviour so that if I have two paths:
"/api/path/123"
I can match above route with this one below.
"api/path/id"
Any regex or pattern matching solution?
Thanks
idi527
December 4, 2018, 9:33am
2
If I understand you correctly, you can add a more specific route first like this
get "/api/path/123", SomeViewController, :do_something_specific
get "/api/path/:id", SomeViewController, :do_something_generic
script
December 4, 2018, 9:45am
3
On a side note. Can we get controller name from conn? where request is dispatching.
idi527
December 4, 2018, 9:59am
4
Probably, inspect
the conn, the controller might be in conn.private.phoenix_controller
but that’s probably an implementation detail and can change in the future …
script
December 4, 2018, 10:03am
5
I tried that but its not there.
idi527
December 4, 2018, 10:19am
6
Have you piped the conn through a router?
1 Like
conn.private.phoenix_controller
should work as expected and should give you the full module name
Rather than conn.private.phoenix_controller
you should use Phoenix.Controller.controller_module/1
iex> Phoenix.Controller.controller_module(conn)
MyApp.UserController
1 Like
btw just in case you not aware Phoenix.Controller.controller_module
also returns conn.private.phoenix_controller
@doc """
Returns the action name as an atom, raises if unavailable.
"""
@spec action_name(Plug.Conn.t) :: atom
def action_name(conn), do: conn.private.phoenix_action
@doc """
Returns the controller module as an atom, raises if unavailable.
"""
@spec controller_module(Plug.Conn.t) :: atom
def controller_module(conn), do: conn.private.phoenix_controller
@doc """
Returns the router module as an atom, raises if unavailable.
"""
@spec router_module(Plug.Conn.t) :: atom
def router_module(conn), do: conn.private.phoenix_router
@doc """
Returns the endpoint module as an atom, raises if unavailable.
"""
axelson
December 5, 2018, 5:53am
10
I am definitely aware of that. But the private
area of the conn is for libraries to put data in that they are in charge of. And the actual data that is stored in there is an implementation detail and should not be relied upon directly, otherwise your code may break in a future version of Phoenix.
you have a valid point about not using private api’s but I was merely correcting what he had stated above
axelson
December 5, 2018, 8:54am
12
Ah, it looked like you were responding to me, but I guess I may have misunderstood. Sorry about that!