【发布时间】:2016-08-28 07:22:33
【问题描述】:
用户访问以下路由:www.example.com/api/users/x,其中 x 是变量。
如何获取 x 并创建具有动态响应的路由?
自动翻译。
【问题讨论】:
用户访问以下路由:www.example.com/api/users/x,其中 x 是变量。
如何获取 x 并创建具有动态响应的路由?
自动翻译。
【问题讨论】:
在您的 router.ex 中,您需要编写如下内容 -
get "/api/users/:x", SomeController, :actionName
现在在你的控制器中,你需要使用模式匹配从_params中获取x的值,即:
def actionName(conn, %{"x" => x}) do
# now x is available here
end
【讨论】: