[英]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.我知道 Phoenix 会为路由
/users/:id
生成像users_path()
这样的前缀帮助器,但是这对我来说似乎users_path()
,因为我们不应该使用以后在路由控制器中工作时可能会更改的前缀。
As of Phoenix 1.3.0.rc.1 you can use current_path
and current_url
to get the current path or url respectively.从Phoenix 1.3.0.rc.1 开始,您可以使用
current_path
和current_url
分别获取当前路径或 url。
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.