Phoenix namespacing and views/templates questions

I created a Phoenix application called ‘hello’. My question is in router.ex

defmodule HelloWeb.Router do

does HelloWeb have anything to do with namespacing or parent directory? Or it can be something else? Same with

defmodule HelloWeb.HelloController do
defmodule HelloWeb.HelloView do

or are they just convention?

Also why do we need a view even if empty to tie controllers to templates in Phoenix?

Thanks for the help!

The namespace of elixir is flat, “namespacing” is just a convention.

Also location in the filesystem or even the name of the file does not matter at all, as long as the compiler can find it.

Because during compiletime the templates get compiled into the view module as functions.

1 Like

So how do I refactor if I change

defmodule HelloWeb.Router do

to

defmodule Router do

or even to

defmodule Somename.Router do

Also did you mean HelloWeb has nothing to do with the directory hello_web?

Yes. Thats only by convention.

I’m not sure about the why… Also depending on you use phoenix or plug directly, the naming conventions might become naming rules due to some magic happening in phoenix.

Also even though no actual namespaces exist, convention says, that you should use your applications name in camelcase as a top level prefix.

There’s not much to it in phoenix. The only implicit generation of module names I’m aware of is that a controller will use the view of the same name just where Controller is replaced with View. If you don’t want it implicitly generated you can always use conn |> put_view(view) |> render(template, assigns) in your controller actions (or put the view earlier). The other instance of where a module name prescribes some other name is for the folder where a view takes its templates from, but this part only ever affects a single view at a time and is also configurable.

2 Likes

Here is the correct answer after my analysis…HelloWeb is required, as simple as that.

This is definitely not true. Please show any errors you got so that we can help diagnose the issue.

I’ve just yesterday renamed basically every MyAppWeb module in a fresh phoenix project (besides the endpoint) so unless you miss to update some references to those modules you can name them however you want.

Well It’s still convenient to easily find the location of a given module knowing its namespace.

Everything can be refactored in any framework or language but for practical purposes HelloWeb is required here.

I’m not native english speaker but “required” seems too strong for me.
I think for practical purposes as you said, you can choose your own module namespace as long as you ensure its uniqueness and the SomeModule format.

For example by default you have HelloWeb.PageController which is located to hello_web/controllers/page_controller.ex. One could name it HelloWeb.Controllers.PageController or ever HelloWeb.Controllers.Page, but I think the default name is nicer… ^^

And one last example, I like to create for example a folder named project_path/lib/hello/extras and name its modules whatever I want as long as I like it, such as Site, Easy… I do that for extra modules I want to call anywhere in the project but with very short namespace, without the Hello namespace part. ^^

All that to say don’t feel forced to keep HelloWeb.

1 Like