Is it possible to group controllers in sails using subfolders?

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

+5
source share
4 answers

Can you do this. The trick is that the controller identifier is its path, in your case admin/PageController . Thus, the user route in config/routes.js will look something like this:

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

The great thing is that automatic actions still work, so if there is an index action in the controller, then browsing to /admin/page will automatically start.

You can also create such controllers using the sails generate controller admin/page .

+18
source

Edit

Since commit 8e57d61 you can do this to get blueprint routes and functionality for nested controllers if your project has an AdminPage model:

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

or that:

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

Old answer

Your options

  • Defining explicit routes for your grouped controllers in config/routes.js . See Scott Grace's answer for more details.

  • (If you are a little adventurous). Since I had the same requirement for my project, I created a Pull Request on sails, which allows you to override the model-controller association. You can install it through

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

    Assuming the api/models/Page.js you need the api/controllers/admin/PageController.js building methods you could do:

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

Explanation

While generating / creating grouped controllers like this is perfectly acceptable, you will not get the default routes that you would expect for controllers followed by models with the same identifier.

eg. api/controllers/UserController.js and api/models/User.js use the same user identifier, so drawing routes are mounted if they are included in config/blueprints.js .

In fact, at the moment it is not possible to correctly group models into subfolders. This means that you cannot create a model that matches the admin/page ID of your controller api/controllers/admin/PageController.js - drawing routes are not set for PageController .

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

+9
source

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

Basically, this exercise is for me to better understand this topic, but I hope this helps someone else. Please let me know if I made a mistake:

enter image description here

+5
source

Thanks to merionebl, his work is great for me and I want to share with all the guys, my answer is from the answer from merionebl.

/config/routes.js

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

My goal is not to repeat the answer, just update and illustrate it.

thanks

0
source

Source: https://habr.com/ru/post/979302/


All Articles