插件结构
¥Plugin structure
当 使用插件 SDK 创建插件 时,Strapi 会在 /src/plugins/my-plugin
文件夹中为你生成以下样板结构:
¥When creating a plugin with Plugin SDK, Strapi generates the following boilerplate structure for you in the /src/plugins/my-plugin
folder:
- TypeScript-based plugins
- JavaScript-based plugins
. # root of the plugin folder (e.g., /src/plugins/my-plugin)
├── admin # Admin panel part of your plugin.
│ ├── src
│ │ ├── components # Contains your front-end components
│ │ │ ├── Initializer.tsx # Plugin initializer
│ │ │ └── PluginIcon.tsx # Contains the icon of your plugin in the main navigation
│ │ ├── pages # Contains the pages of your plugin
│ │ │ ├── App.tsx # Skeleton around the actual pages
│ │ │ └── HomePage.tsx # Homepage of your plugin
│ │ ├── translations # Translations files to make your plugin i18n-friendly
│ │ │ ├── en.json
│ │ ├── utils
│ │ │ └── getTranslations.ts # getTranslations function to return the corresponding plugin translations
│ │ ├── index.ts # Main setup of your plugin, used to register elements in the admin panel
│ │ └── pluginId.ts # pluginId variable computed from package.tsxon name
│ ├── custom.d.ts # Generated types
│ ├── tsconfig.build.json
│ └── tsconfig.json # TypeScript compiler options for the admin panel part
├── dist # Build of the plugin (front end and back end)
├── node_modules
├── server # Back-end part of your plugin
│ ├── src
│ │ ├── config
│ │ │ └── index.ts # Contains the default server configuration
│ │ ├── content-types # Content-types specific to your plugin
│ │ │ └── index.ts # Loads all the plugin's content-types
│ │ ├── controllers # Controllers specific to your plugin
│ │ │ ├── index.ts # Loads all the plugin's controllers
│ │ │ └── controller.ts # Custom controller example. You can rename it or delete it.
│ │ ├── middlewares # Middlewares specific to your plugin
│ │ │ └── index.ts # Loads all the plugin's middlewares
│ │ ├── policies # Policies specific to your plugin
│ │ │ └── index.ts # Loads all the plugin's policies
│ │ ├── routes # Routes specific to your plugin
│ │ │ └── index.ts # Contains an example route for the my-controller custom controller example
│ │ ├── services # Services specific to your plugin
│ │ │ ├── index.ts # Loads all the plugin's services
│ │ │ └── service.ts # Custom service example. You can rename it or delete it.
│ │ ├── bootstrap.ts # Function that is called right after the plugin has registered
│ │ ├── destroy.ts # Function that is called to clean up the plugin after Strapi instance is destroyed
│ │ ├── index.ts # Entrypoint for the server (back end)
│ │ └── register.ts # Function that is called to load the plugin, before bootstrap.
│ ├── tsconfig.build.json
│ └── tsconfig.json # TypeScript compiler options for the server part
├── .editorconfig
├── .eslintignore
├── .gitignore
├── .prettierignore
├── .prettierrc
├── package-lock.json
├── package.json
└── README.md
. # root of the plugin folder (e.g., /src/plugins/my-plugin)
├── admin # Admin panel part of your plugin.
│ ├── src
│ │ ├── components # Contains your front-end components
│ │ │ ├── Initializer.jsx # Plugin initializer
│ │ │ └── PluginIcon.jsx # Contains the icon of your plugin in the main navigation
│ │ ├── pages # Contains the pages of your plugin
│ │ │ ├── App.jsx # Skeleton around the actual pages
│ │ │ └── HomePage.jsx # Homepage of your plugin
│ │ ├── translations # Translations files to make your plugin i18n-friendly
│ │ │ ├── en.json
│ │ ├── utils
│ │ │ └── getTranslations.js # getTranslations function to return the corresponding plugin translations
│ │ ├── index.js # Main setup of your plugin, used to register elements in the admin panel
│ │ └── pluginId.js # pluginId variable computed from package.json name
│ └── jsconfig.json
├── dist # Build of the plugin (front end and back end)
├── node_modules
├── server # Back-end part of your plugin
│ ├── src
│ │ ├── config
│ │ │ └── index.js # Contains the default server configuration
│ │ ├── content-types # Content-types specific to your plugin
│ │ │ └── index.js # Loads all the plugin's content-types
│ │ ├── controllers # Controllers specific to your plugin
│ │ │ ├── index.js # Loads all the plugin's controllers
│ │ │ └── controller.js # Custom controller example. You can rename it or delete it.
│ │ ├── middlewares # Middlewares specific to your plugin
│ │ │ └── index.js # Loads all the plugin's middlewares
│ │ ├── policies # Policies specific to your plugin
│ │ │ └── index.js # Loads all the plugin's policies
│ │ ├── routes # Routes specific to your plugin
│ │ │ └── index.js # Contains an example route for the my-controller custom controller example
│ │ ├── services # Services specific to your plugin
│ │ │ ├── index.js # Loads all the plugin's services
│ │ │ └── service.js # Custom service example. You can rename it or delete it.
│ │ ├── bootstrap.js # Function that is called right after the plugin has registered
│ │ ├── destroy.js # Function that is called to clean up the plugin after Strapi instance is destroyed
│ │ ├── index.js # Entrypoint for the server (back end)
│ │ └── register.js # Function that is called to load the plugin, before bootstrap.
├── .editorconfig
├── .eslintignore
├── .gitignore
├── .prettierignore
├── .prettierrc
├── package-lock.json
├── package.json
└── README.md
Strapi 插件分为 2 部分,每个部分位于不同的文件夹中并提供不同的 API:
¥A Strapi plugin is divided into 2 parts, each living in a different folder and offering a different API:
插件部分 | 描述 | 文件夹 | API |
---|---|---|---|
管理面板 | 包括 管理面板 中可见的内容(组件、导航、设置等) | admin/ | 管理面板 API |
后端服务器 | 包括与 后端服务器 相关的内容(内容类型、控制器、中间件等) | server/ | 服务器 API |
仅服务器插件:你可以创建一个仅使用服务器部分的插件来增强应用的 API。例如,这个插件可以有自己的可见或不可见的内容类型、控制器操作和对特定用例有用的路由。在这种情况下,你不需要插件在管理面板中具有界面。
¥Server-only plugin: You can create a plugin that will just use the server part to enhance the API of your application. For instance, this plugin could have its own visible or invisible content-types, controller actions, and routes that are useful for a specific use case. In such a scenario, you don't need your plugin to have an interface in the admin panel.
管理面板插件与特定于应用的定制:你可以创建一个插件来将一些组件注入管理面板。但是,你也可以通过创建
/src/admin/index.js
文件并调用bootstrap
生命周期函数来注入组件来实现此目的。在这种情况下,决定是否创建插件取决于你是否计划重用和分发代码,或者它是否仅对独特的 Strapi 应用有用。¥Admin panel plugin vs. application-specific customization: You can create a plugin to inject some components into the admin panel. However, you can also achieve this by creating a
/src/admin/index.js
file and invoking thebootstrap
lifecycle function to inject your components. In this case, deciding whether to create a plugin depends on whether you plan to reuse and distribute the code or if it's only useful for a unique Strapi application.
Strapi 插件开发之旅的下一步将要求你使用任何 Strapi 插件 API。
¥The next steps of your Strapi plugin development journey will require you to use any of the Strapi plugins APIs.
2 种不同类型的资源可帮助你了解如何使用插件 API:
¥2 different types of resources help you understand how to use the plugin APIs:
管理面板 API 和 服务器 API 的参考文档概述了 Strapi 插件的功能。
¥The reference documentation for the Admin Panel API and Server API give an overview of what is possible to do with a Strapi plugin.
指南 涵盖了一些特定的、基于用例的示例。
¥Guides cover some specific, use-case based examples.