Strapi V4 search by slug instead ID

It’s way simplier thant that

  1. Create route file in the “collection” folder, in my case project/routes
//project/routes/01-custom-project.ts
export default {
  routes: [
    {
      method: "GET",
      path: "/projects/:slug",
      handler: "project.findOne",
    },
  ],
};

  1. Replace core function findOne
//project/controllers/project.ts
"use strict";

/**
 * project controller
 */

import { factories } from "@strapi/strapi";

export default factories.createCoreController(
  "api::project.project",
  ({ strapi }) => ({
    async findOne(ctx) {
      const { slug } = ctx.params;

      const query = {
        filters: { slug },
        ...ctx.query,
      };

      const project = await strapi.entityService.findMany(
        "api::project.project",
        query
      );

      const sanitizedEntity = await this.sanitizeOutput(project, ctx);

      return this.transformResponse(sanitizedEntity[0]);
    },
  })
);