Hello,
I’m currently facing a challenge with my Phoenix app, which displays statistics about the products we support. I’m extending its functionality to allow users to specify a date range route parameter to filter the statistics. My goal is to define /stats
and /stats/:range
in my router.
However, I already have a /stats/:country
route that takes a two-character ISO country code, such as /stats/AT
for Austrian statistics.
live "/stats", StatisticsLive.General
live "/stats/:country", StatisticsLive.ByCountry
live "/stats/:range", StatisticsLive.General
These two routes now conflict with each other, and I can’t define constraints like in Rails using the :constraints
option (at least that I know of).
I now tried the following, because I have a fixed list of countries:
live "/stats", StatisticsLive.General
for country <- Countries.all() do
live "/stats/#{country}", StatisticsLive.ByCountry, String.to_existing_atom(country)
end
live "/stats/:range", StatisticsLive.General
This works by misusing the live_action
value passed to the LiveView. However, I need to convert the values to atoms back and forth, can’t access the value via the params
map, don’t see it in logs, and overall, it just doesn’t feel right.
Is there a reason why this syntax isn’t currently supported?
for country <- Countries.all() do
live "/stats/#{country}", StatisticsLive.ByCountry, params: %{country: country}
end
I don’t want to introduce a custom plug for this, as it reduces the clarity and visibility of my routes.