Pattern matching with regex

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

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

On a side note. Can we get controller name from conn? where request is dispatching.

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 …

I tried that but its not there.

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

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

Ah, it looked like you were responding to me, but I guess I may have misunderstood. Sorry about that!