【问题标题】:Dynamic routes in Phoenix FrameworkPhoenix 框架中的动态路由
【发布时间】:2016-08-28 07:22:33
【问题描述】:

用户访问以下路由:www.example.com/api/users/x,其中 x 是变量。

如何获取 x 并创建具有动态响应的路由?

自动翻译。

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    在您的 router.ex 中,您需要编写如下内容 -

    get "/api/users/:x", SomeController, :actionName
    

    现在在你的控制器中,你需要使用模式匹配从_params中获取x的值,即:

    def actionName(conn,  %{"x" => x}) do
        # now x is available here
    end
    

    更多详情,http://www.phoenixframework.org/docs/controllers

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2012-03-13
    • 2020-01-27
    • 2015-12-23
    • 1970-01-01
    相关资源
    最近更新 更多