5

I'm planning to organize my controllers in sails using subfolder but I'm not sure how to do it. When I tried using like admin/PageController.js and connect it with the route I keep getting a 404 error.

4 Answers 4

18

You can definitely do this. The trick is, the controller identity is its path, in your case admin/PageController. So a custom route in config/routes.js would be something like:

'GET /admin/page/foo': 'admin/PageController.foo'

The great thing is, automatic actions still work, so if you have an index action in the controller then browsing to /admin/page will automatically run it.

You can also create controllers like this with sails generate controller admin/page.

4
  • 1
    Hello Scott, I've tried doing this but I'm getting a 404 error. When I run sails lift --silly I can't see the /admin/page/foo path. Thanks
    – ginad
    Feb 27, 2014 at 14:17
  • Ok, I created a new app using sails new with rc3, then sails generate controller admin/page foo, and was able to lift and hit that route. Maybe post a gist with your controller and routes.js file?
    – sgress454
    Feb 28, 2014 at 18:30
  • Thanks, I just pulled the latest version of 0.10 and everything is working fine now.
    – ginad
    Mar 3, 2014 at 3:45
  • Note: You can also use the nested controller path name as a policy key. config/policies.js - 'admin/PageController' : { '*': 'authorized', 'foo':'isFoo' }
    – dbasch
    Aug 21, 2014 at 19:09
9

Edit

Since commit 8e57d61 you can do this to get blueprint routes and functionality on nested controllers, assuming there is an AdminPage model in your project:

// api/controllers/admin/PageController.js
module.exports = {
  _config: {
    model: 'adminpage'
  }
}

or this:

// config/routes.js
module.exports.routes = {
  'admin/page': {
    model: 'adminpage'
  }
}

Old Answer

Your options

  1. Defining explicit routes to your grouped controllers in config/routes.js. Look at Scott Gress' answer for more details.

  2. (If you are a bit adventurous) As i had the exact same requirement for a project of mine I created a Pull Request on Sails that allows you to override the model - controller association. You could install it via

    npm install -g git://github.com/marionebl/sails.git#override-controller-model
    

    Assuming it is the api/models/Page.js model you want the blueprint methods for on api/controllers/admin/PageController.js you then could do:

    // api/controllers/admin/PageController.js
    ...
    module.exports = {
      _config: {
        model: 'page'
      }
    }
    

Explanation

While generating/creating grouped controllers like this is perfectly valid an possible, you will not get the default blueprint routes you'd expect for controllers accompanied by models with the same identity.

E.g. api/controllers/UserController.js and api/models/User.js share the same identity user, thus the blueprint routes are mounted if they are enabled in config/blueprints.js.

In fact at the moment it is not possible to group models into subfolders in a valid way. This means you won't be able to create a model that matches the identity admin/page of your controller api/controllers/admin/PageController.js - the blueprint routes are not mounted for PageController.

The source responsible for this behavior can be inspected on Github.

1
  • Thank you so much for your help, as per my experiment it will work with me fine. routes.js { controller: "admin/UserController", action: "find", model : 'user', //NOTE : Model name should be small letter }, Jul 2, 2016 at 5:28
6

I made a diagram that shows how implicit routes, explicit policies, nested controllers, singular models and nested views are related. It does not show an overridden model-controller association as described by @marionebl.

It was mostly an exercise for me to understand this topic better, but I hope it helps somebody else too. Please let me know if I made any mistakes:

enter image description here

1
  • Thanks for making a great product @mikermcneil !
    – dbasch
    Feb 4, 2016 at 11:50
0

Thanks merionebl, its work fine for me and I want to share with all guys my answer derived from merionebl answer.

/config/routes.js

  'get /admin/user' : { 
    controller: "Admin/UserController", action: "find",
    model : 'user',
  },

My aim is not repeat answer just have upgrade and clear example.

Thanks

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.