Is it possible to group controllers in sails using subfolders?

前端 未结 4 1240
囚心锁ツ
囚心锁ツ 2020-12-11 06:53

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 rou

4条回答
  •  星月不相逢
    2020-12-11 07:00

    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.

提交回复
热议问题