Skip to content

Instantly share code, notes, and snippets.

@SergioJuniorCE
Last active November 30, 2021 21:07
Show Gist options
  • Save SergioJuniorCE/c7922de18cddbbe91226a52ebca06065 to your computer and use it in GitHub Desktop.
Save SergioJuniorCE/c7922de18cddbbe91226a52ebca06065 to your computer and use it in GitHub Desktop.
Add slug to strapi param
  • Go to api folder
  • Select the collection you want to add the slug to
  • Go to config folder
  • Open routes.json
  • Add this code to routes array:
      {
        "method": "GET",
        "path": "/collection/:slug",
        "handler": "collection.findOne",
        "config": {
          "policies": []
        }
      },
  • Go to controllers folder
  • Go to collection.js
  • Paste this code and replace collection with your collection name
    'use strict';
    const { sanitizeEntity } = require('strapi-utils');
    /**
     * Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
     * to customize this controller
     */
    
     module.exports = {
      /**
       * Retrieve a record.
       * 
       * @return {Object}
       */
    
      async findOne(ctx) {
        const { slug } = ctx.params;
        const collection = await strapi.services.collection.findOne({ slug });
        return sanitizeEntity(category, { model: strapi.models.collection });
      }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment