System Information
- 4.0:
- Ubuntu:
- Database:
- Node Version:
- NPM Version:
- Yarn Version:
I meant to search by slug instead of id, the ids are by order of arrival so it doesn’t help me when searching for them.
I found one way which is to create a controller and a route, but I don’t know if there is a more efficient way to do it.
Example: /api/productos/:slug
In case someone is reading this, I found this way to do it in strapi 4.0 in case someone finds it useful
The bad thing is that the other controllers are disabled, plus I have not found a way to update them. But otherwise it works, I hope it works for you.
Controller:
module.exports = createCoreController('api::inventario-usa.inventario-usa', ({ strapi }) => ({
async findUid(ctx) {
const { slug} = ctx.params;
console.log(slug);
const response= await strapi.db.query('api::your-collection').findOne({
where: {
Code:slug,
},
});
return response;
}));
Route:
module.exports = {
routes: [
{
method: 'GET',
path: '/search/product/:slug',
handler:'your-collection.findUid',
}
]
}
can you please explain a bit more about routes?
are we customizing the routes in the api/content-type/routes ?
'use strict';
/**
* post router.
*/
const { createCoreRouter } = require('@strapi/strapi').factories;
module.exports = createCoreRouter('api::post.post');
@contactvinodc - To define your custom routes and controller along with keeping the default routes, you need to create a new file within routes folder. I have achieved it with following structure:-
In the custom router file, you can define routes like this
To map it to the custom controller, you can follow below in custom controller file:-
Hope this helps.