0
votes

I've recently started working with Elixir/Phonix (love it) and I would like to understand how 'Magic' paths (as I refer to them) work and which paths are automagically configured for Phoenix and where they point to.

By 'Magic' paths I am referring to code like this: to: activity_path(@conn, :index) or to: user_path(@conn, :update) or static_path(@conn, "/js/bootstrap.min.js").

It appears that I can use a 'Magic` path for any controller module I create, i.e.

defModule MyWeb.HoobitzController do
  ...
end

so I can call a function in the HoobitzController by using hoobitz_path().

Does this only work for controllers? Does it go by the Controller name or the controller's file name?

static_path() apparently points to the web root's /priv directory and I assume it is the Phoenix core that establishes this reference in some mystical way. Are there any other 'Magic' paths pointing to other locations within the web root? If so, what are they and where do they point to?

Finally, is there a 'real' name for these 'Magic' paths? What term does the community refer use to refer to them?

2

2 Answers

0
votes

For static_path() you have correctly answered your question yourself.

For what you call magic paths, they come from your lib/yourapp_web/router.ex file and might be listed with mix phx.routes task from the command line. I believe they are called “route paths.”

You might check how it’s being constructed in Phoenix code.

0
votes

static_path and Plug.Static

The static_path/1 is the callback function which is defined in Phoenix.Endpoint behaviour. Look inside your YOUAPP.Endpoint module, there is "use Phoenix.Endpoint, otp_app: :your_app_web``` at the top of module. When you look at source code for static_path/1, it actually returns "/" as a default value for script path. Basically, Phoenix only generate the path for you and it does not know the location of your web app /priv directory.

Inside your Endpoint module, there should be a section of code like this: plug Plug.Static, at: "/", from: :tokenix_admin_web, gzip: false, only: ~w(css fonts images js favicon.ico robots.txt)

The keyword from which is 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`). The Plug.Static module is the one serving the priv/static contents when there is a request to "/" path.

Phoenix.Router.Helpers

The Magic paths or generated paths are from Phoenix.Router.Helpers module. Before Phoenix.Router compile, it will define helpers function first. It read your routes definitions can pass it into Phoenix.Router.Helpers.define/2 which will generated based on the controller name. 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" It uses metaprogramming to generate helper_path code. So the real name is helper path.

You should take a look at your YourApp, YourApp.Application", YourApp.Endpoint and YourApp.Router modules. Phoenix usually will explicitly define the configurations.