Pattern matching with regex

regex

#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


#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

#3

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


#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 …


#5

I tried that but its not there.


#6

Have you piped the conn through a router?


#7

conn.private.phoenix_controller should work as expected and should give you the full module name


#8

Rather than conn.private.phoenix_controller you should use Phoenix.Controller.controller_module/1

iex> Phoenix.Controller.controller_module(conn)
MyApp.UserController

#9

btw just in case you not aware Phoenix.Controller.controller_module also returns conn.private.phoenix_controller


#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.


#11

you have a valid point about not using private api’s but I was merely correcting what he had stated above


#12

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