简体   繁体   中英

Phoenix - Get current path in controller

How can I get the current route path from within the controller?

I know Phoenix generates prefixed helpers like users_path() for a route /users/:id , however that does not seem right for my case because we shouldn't have to use a prefix that may later change when working within the route's controller.

As of Phoenix 1.3.0.rc.1 you can use current_path and current_url to get the current path or url respectively.

  iex> current_url(conn)
  "https://www.example.com/users/123?existing=param"

  iex> current_url(conn, %{new: "param"})
  "https://www.example.com/users/123?new=param"

  iex> current_url(conn, %{})
  "https://www.example.com/users/123"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM