如何为用户添加自定义路由 [英] How to add custom route for user
问题描述
我创建了一个包含书籍的 books
内容类型.集合中的每本书都属于一个用户(用户内容类型由 Strapi 提供).
I have created a books
content type containg books. Each book in the collection belongs to a user (user content type provided by Strapi).
我想在 /users/me/books
端点返回经过身份验证的用户拥有的书籍列表.我在哪里可以添加此路由和处理程序,因为 /api/books
目录包含与书籍相关的路由、控制器等,但不包含 /api/users
目录.
I want to return list of books owned by authenticated user at /users/me/books
endpoint. Where can I add this route and handler as there is /api/books
directory containing books related route, controllers, etc. but not /api/users
directory.
推荐答案
您可以使用扩展系统进行扩展或覆盖.
You can extend or override using the extensions system.
extensions/users-permissions/controllers
只需将您想要扩展或覆盖的控制器添加为 .js 文件,如下所示:
Just add the controller you want to extend or override as a .js file like so:
因此要覆盖 User.js
下的 me
端点,您只需要再次定义该方法:
So to override the me
endpoint under User.js
you only need to define the method again:
'use strict';
module.exports = {
//Override me
async me(ctx) {
//do your thing
}
};
扩展,而不是覆盖,意味着添加另一个端点,因此您需要定义它,添加路由并为其设置权限.应该在以下位置创建 routes.js 文件:
To extend, not override, means to add another endpoint, therefor you need to define it, add a route and set permissions for it. The routes.js files should be created at:
extensions/users-permissions/config/routes.json
像这样:
{
"routes": [
{
"method": "GET",
"path": "/users/me/books",
"handler": "User.getUserBooks",
"config": {
"policies": [],
"prefix": "",
"description": "description",
"tag": {
"plugin": "users-permissions",
"name": "User",
"actionType": "find"
}
}
}
}
这次的控制器(与开始的位置相同):
The controller this time (same location as in beginning):
module.exports = {
async getUserBooks(ctx) {
//add logic
}
}
OP 正确添加:
添加自定义路由和控制器后,必须进入管理面板(以管理员身份登录)>角色和权限>用户权限.在那里您可以找到新添加的路由,并且必须通过检查启用它.
After adding custom route and controller, one has to go to Admin Panel(log in as admin)>Roles and Permission> Users-Permission. There you can find the newly added route and have to enable it by checking it.
原件(如果您需要示例)位于:
The originals(if you need examples) are located at:
/node_modules/strapi-plugin-users-permissions/config/routes.json
/node_modules/strapi-plugin-users-permissions/controllers/User.js
我认为您不应该扩展 User 控制器,因为它在逻辑上不正确.您正在尝试获取书籍 - 您应该以相同的方式扩展书籍 api.据我所知,ContentType
不包含有关其创建者的信息(如果不正确,欢迎您告诉我).所以为了解决这个问题,你可以添加到你的 ContentType
books"与 User
的关系.然后我认为您应该使用返回书籍归属"的端点来扩展书籍 api.使用收到的 ctx
发送给该用户.
I don't think you should extend the User controller as it isn't logically correct. You are trying to GET books - you should extend the book api in the same way.
From what I can tell a ContentType
doesn't contain information about its creator(you're welcome to educate me if it's not true).
So to tackle that you can add to your ContentType
"books" a relation to User
.
Then I think you should extend the books api with a endpoint that returns books "belonging" to that user using the ctx
received.
另外 - 检查这个问题
如果您需要更多信息,请发表评论.
Comment if you need more info.
这篇关于如何为用户添加自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!