Override strapi-admin 'restaurant' (in my case post) creation, update or what ever controllers

Hello fellas,
I’m working on controllers to modify the default process of a creation, update, … what ever but I was a bit surprise to notice that controllers inside the api folder aren’t called by the strapi-admin…. can I do something to override then ?

To be more accurate when I use insomnia and make a post request at the following url

http://localhost:1337/posts

I scop on the create controller which is customized in the api folder as expected. When I create a post or restaurant (what ever is your collection) from the strapi-admin it’s look like the create controller which is used isn’t mine I suppose the default one override mine or most probably mine, doesn’t override the default controller…

Hopefully I’m enough clear and somebody will help me to understand what’s going on :wink:

regards

Hi, I ran into the exact same surprise. I’m wondering if v4 will change this.

So I searched a lot with a help of @mbule (on the strapi discord) We tried to pass trough the service but look like the strapi-admin does not call the service’s functions too… bit sad…

So @mbule suggest to use Model Lifecycle Hook to change the default field comportement and customize the intput value like an realtime update(that how I understood it in the doc).

So In my case, I wanted to parse the body and auto build the slug field based on the title, in the following code that what I do :

// api/post/models/post.js
const slugify = require("slugify");
// utils
const buildFullUrl = require("../../../utils/buildFullUrl");

module.exports = {
  /**
   * Triggered before user creation.
   */
  lifecycles: {
    async beforeCreate(data) {
      if (data.title) {
        data.slug = slugify(data.title, { lower: true });
      }
      if (data.body) {
        // function which modify the hyperLinks and return HMLT body as string
        const body = await buildFullUrl.index(data);
        data.body = body;
      }
    },
    async beforeUpdate(params, data) {
      if (data.title) {
        data.slug = slugify(data.title, { lower: true });
      }
      if (data.body) {
        // function which modify the hyperLinks and return HMLT body as string
        const body = await buildFullUrl.index(data);
        data.body = body;
      }
    },
  },
}; 

@mbule Hopefully my explication is enough clear ^^

Hopefully this post gonna help some of you !
regards

1 Like