I have an application with subdomains, and I need to construct the full URL for a liveview component (the full link needs to be e-mailed to a user)
For regular controllers, I used something like the following:
url = conn
|> Routes.my_controller_url_url(:edit, &1)
What would be the best way to do something similar for a socket? I can’t seem to find any pointers in the docs.
Any suggestions would be really helpful, thanks!
Usually for all things routing related you can simply use your
MyAppWeb.Endpoint module anywhere that a
conn would be supplied.
@benwilson512 Thanks for your reply, this does not render the subdomain for me unfortunately.
The following code only renders the host defined in the endpoint, not the current subdomain.
url = Routes.my_controler_url(MyApPWeb.Endpoint, :edit, "param")
I suspect I need to work with the socket to get the current subdomain, but I’m not sure how.
Try something like:
|> Routes.my_controller_url(:edit, "param")
|> then(&URI.merge(socket.host_uri, &1))
That should give you the current URL.
You can pass a
URI as arguments to your routes too. So you can keep a
@url assign in your LiveView, with the subdomain, and pass it in instead of
I work on phoenix websites using multiples domains.
I made a gist to show how we handle multiples domains.
I like it how everyone adds a piece to the puzzle!
The final one-liner I settled on:
url = Routes.my_controller_url(socket.host_uri, :edit, "param")
Thanks @moogle19, @josevalim, @MRdotB!