user6902625
user6902625

Reputation:

Similar controllers in different namespaces/scopes -- how to distinguish between them?

I have 2 similar controllers in different scopes/folders:

MyApp.Controller1
MyApp.Namespace1.Controller1

and

  # mix phoenix.routes
  some_path  GET /             MyApp.HomeController1 :index
  some_path  GET /namespace1   MyApp.Namespace1.Controller1 :index

How can I refer to :index in _url or _path helpers in these controllers?

Upvotes: 1

Views: 252

Answers (2)

tkowal
tkowal

Reputation: 9289

This should be a comment to previous answer, but it was too long. There is a second option to name the route in your web/router.ex:

scope "/namespace1" do
  get "/", MyApp.Namespace1.Controller1, :index, as: "namespaced"
end

Now you should be able to use some_path(conn, :index) and namespaced_path(conn, :index) without passing the module name.

Upvotes: 2

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

According to your phoenix.routes, these calls respectively:

some_path(MyApp.Endpoint, :index)
some_path(MyApp.Namespace1.Endpoint, :index)

See routing chapter for details.

Upvotes: 1

Related Questions