Core: Custom namespace for controllers

Created on 25 Apr 2016  Â·  11Comments  Â·  Source: adonisjs/core

Is there any way to use a controller that isn't physically in the "app/Http/Controllers" directory? It appears as though the controller namespace is hard-coded in:
node_modules\adonis-framework\src\Server\index.js

I'm setting up the route like so:
Route.get('/', 'Web/Subdomains/Landing/Http/Controllers/HomeController.index')

The same applies to the Listeners path, and possibly/probably other places. Could these be set in the config?

The reasoning behind this is that I've got several "modules" (subdomains or components such as auth) and I would like to group my controllers, middleware and web-side services into separate namespaces. Here's a simplified version of my folder structure:

    app/
    |-- Modules/
        |-- Blog/
            |-- Console/
            |-- Http/
                |-- Controllers/
                |-- Middleware/
            |-- Model/
            |-- Providers/
            |-- Resources/
                |-- Lang/
                |-- Views/

I did attempt to add a relative paths when setting up the route above (prepending with ../../, which seemed a little messy, but livable), but it threw an error in the Ioc because it expected the only "." to separete the class and method.

I'd just like to say, great work with the project. It's a thing of beauty! I'm using dev 3.0.

Most helpful comment

Start your namespaces with App. So it will be
Route.get('/', 'App/Web/Subdomains/Landing/Http/Controllers/HomeController.index') and i believe your Web directory is within the app directory

All 11 comments

Start your namespaces with App. So it will be
Route.get('/', 'App/Web/Subdomains/Landing/Http/Controllers/HomeController.index') and i believe your Web directory is within the app directory

Well, that was a stupid, stupid mistake. Sorry to bother you!

On a side note, I had been having problems with the Ioc container as well, but couldn't find out why for a while. Apparently, I copied the docs Controller example without manually adding in 'use strict'. The package "require-stack" kept throwing "cannot read property 'toString()' of undefined" errors because it kept trying to check() the file which couldn't be loaded, and would only abort if the exception thrown by require() was not 'SyntaxError' (which it was). Minor bug caused a bug, but thought I'd report it.

Thanks for your quick response. And sorry for wasting your time!

I have a folder inside Controllers named Admin

app/
    |-- Http/
        |-- Controllers/
            |-- Admin/
                OrganisationController.js

None of these are working:

Route.resource('organisations', 'Admin/OrganisationController')
Route.resource('organisations', 'App/Http/Controllers/Admin/OrganisationController')

But when I place the same file in the controllers folder:

app/
    |-- Http/
        |-- Controllers/
            OrganisationController.js

This works:

Route.resource('organisations', 'OrganisationController')

There are no docs as well for this 😢
http://adonisjs.com/docs/3.2/controllers

What is the route you are calling. It works fine for me with

Route.resource('organisations', 'Admin/OrganisationController')
class OrganisationController {

  * index (request, response) {
    response.send('organisations')
  }

}

module.exports = OrganisationController

on http://localhost:3333/organisations url

Also please create new issues and do not reply on closed one's, they are likely to get missed

Sorry my bad.
I accidentally created 2 OrganisationController and left the main one blank 😞

So does it work fine now?

Yup!!!

Cool! Closing the issue

while controller files is not within the app directory?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PC-HUB picture PC-HUB  Â·  4Comments

dezashibi picture dezashibi  Â·  4Comments

themodernpk picture themodernpk  Â·  3Comments

devcaststudio picture devcaststudio  Â·  3Comments

begueradj picture begueradj  Â·  3Comments