static_path 和 Plug.Static
static_path/1 是 Phoenix.Endpoint 行为中定义的回调函数。查看YOUAPP.Endpoint 模块内部,模块顶部有“use Phoenix.Endpoint, otp_app: :your_app_web”。当你查看source code for static_path/1 时,它实际上返回“/”作为脚本路径的默认值.
基本上,Phoenix 只会为您生成路径,它不知道您的网络应用程序 /priv 目录的位置。
在您的 Endpoint 模块中,应该有一段代码如下:
plug Plug.Static,
at: "/", from: :tokenix_admin_web, gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)
关键字from 是
the file system path to read static assets from. It can be either: a string containing a file system path, an
atom representing the application name (where assets will
be served from `priv/static`), or a tuple containing the
application name and the directory to serve assets from (besides `priv/static`).
当有对“/”路径的请求时,Plug.Static 模块是提供 priv/static 内容的模块。
Phoenix.Router.Helpers
魔法路径或生成路径来自 Phoenix.Router.Helpers 模块。
Phoenix.Router 编译前会define helpers function first。它读取您的路由定义可以将其传递给Phoenix.Router.Helpers.define/2,它将根据控制器名称生成。
Helpers are automatically generated based on the controller name.
For example, the route:
get "/pages/:page", PageController, :show
will generate the following named helper:
MyAppWeb.Router.Helpers.page_path(conn_or_endpoint, :show, "hello")
"/pages/hello"
MyAppWeb.Router.Helpers.page_path(conn_or_endpoint, :show, "hello", some: "query")
"/pages/hello?some=query"
MyAppWeb.Router.Helpers.page_url(conn_or_endpoint, :show, "hello")
"http://example.com/pages/hello"
MyAppWeb.Router.Helpers.page_url(conn_or_endpoint, :show, "hello", some: "query")
"http://example.com/pages/hello?some=query"
它使用元编程来generate helper_path code。
所以真正的名字是helper path。
您应该查看您的 YourApp、YourApp.Application"、YourApp.Endpoint 和 YourApp.Router 模块。Phoenix 通常会明确定义配置。