Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic routes in Phoenix Framework

Tags:

The user accesses the following route: www.example.com/api/users/x, where x is variable.

How to get x and create routes with dynamic responses?

Automatically translated.

like image 410
Cláudio Júlio Avatar asked Aug 27 '16 23:08

Cláudio Júlio


People also ask

Is Phoenix framework fast?

For any company that currently uses Ruby on Rails, Phoenix Framework should be on your radar, because it adds considerable performance gains. To top that off, Phoenix makes it incredibly fast to build web applications.


1 Answers

In your router.ex you need to write something like following -

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

Now in your controller, you need to use patten matching to get the value of x from _params, ie:

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

For more details, http://www.phoenixframework.org/docs/controllers

like image 170
Tahmina Khatoon Avatar answered Sep 24 '22 16:09

Tahmina Khatoon