raarts
raarts

Reputation: 2961

using controllers from multiple modules in scope section

I am trying to build an Oauth2 server (called Oauth2srv) using shield. But that is not what the question is about. The example code basically says to do this:

scope "/", Shield do
  pipe_through :api

  get "/apps", AppController, :index
  .. etcetera ..
end

The Shield module is in a dependency, so resides in deps/shield. All the routes are there too. Now I want to add a route to a controller in my own module like this:

scope "/", Shield do
  pipe_through :api

  get "/apps", AppController, :index
  get "/*", Oauth2srv.CatchallController, :catch_it
end

The CatchallController resides in web/controllers/catchall_controller.ex. But the scope seems to expect all controllers in the same directory, and the compiler throws me an error: function Shield.Oauth2srv.CatchallController.init/1 is undefined.

What do I need to do?

Upvotes: 0

Views: 190

Answers (1)

Justin Wood
Justin Wood

Reputation: 10041

Most likely, you will want to use a different scope. When you did

scope "/", Shield do
  get "/apps", AppController, :index
end

You are saying that you have a module named Shield.AppController, so when you added the catch all route inside the Shield scope, you are telling the compiler that you have a Shield.CatchallController module.

Though, according to your error, the compiler is looking for a Shield.Oauth2srv.CatchallController (note the Oauth2srv). So you either didn't give all of the information, or there is something else happening.

If these are the only 2 routes that you are defining in your application, you can do something like

scope "/" do
  get "/apps", Shield.AppController, :index
  get "/*", MyApp.CatchallController, :catch_it
end

If you have more, you may want to specify multiple scopes. Something like

scope "/", Shield do
  get "/apps", AppController, :index
  get "/something_else, OtherController, :foo
  ...
end

scope "/", MyApp do
  get "/*", CatchallController, :catch_it

  # Other routes that are important to your application
  ...
end

Upvotes: 1

Related Questions