'How to override Strapi Admin panel endpoints?
In the admin panel - I have a content-type products
, which shows me all the products in the database (MongoDB)
Suppose I would like to edit a product
and when I click on the Save
button, I would like to hit a custom API/ override the existing endpoint that updates my
products
collection!
Is it possible to customize or override the admin panel APIs?
Solution 1:[1]
Update(v4): Judging by the comments - this answer is now outdated as things have changed for v4.
The Strapi admin(strapi-admin) and Content manager(strapi-plugin-content-manager) are defined separately.
If you'd like to override/extend the endpoints; do so by defining the functions you want to override like so:
Locate the controller at:
extensions/<plugin-name>/controllers/<controller-to-override-name>.js
Example:
extensions/content-manager/controllers/ContentManager.js
Notice that the "strapi-plugin" part of the name was dropped!
Example of how to override and extend in controller file:
'use strict';
module.exports = {
//this is how you destroy count
async count(ctx) {
ctx.body = {
count: 99
};
},
//this is how you extend
async aFuncThatDoesntExistInOriginalController(ctx) {
//add logic
},
};
If you're extending you must add a mapping from route to controller & method
To see which endpoint is mapped to which method in the controller check out:
node_modules/<plugin-name>/config/routes.json
node_modules/strapi-plugin-content-manager/config/routes.json
Some basic info in docs: controllers, customization1 & customization2 and FAQ
Solution 2:[2]
Just to add on ghosh's answer. You have to copy also the dependencies on which ContentManager.js
relies on to avoid any errors as follows:
extensions/content-manager/ ???? controllers | ? - ContentManager.js | ???? validation/ | - index.js | - model-configuration.js | ???? services/ | ???? utils/ | ???? configuration/ | - attributes.js ? ???? utils/ - parse-multipart.js
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | saad-mb |