Plugin Development Guide
Overview
Plugin Principle
InnoShop plugins are based on Laravel's automatic discovery and bootstrap mechanism, ensuring that all components of the plugin can be recognized and loaded by the system. The PHP code of the plugin follows the PSR-4 autoloading standard, meaning that directory names and class names should use StudlyCaps (capital camel case naming convention).
Plugin Directory Structure
A typical plugin directory structure is as follows:
Note: In the directory structure of InnoShop plugins, the Routes directory needs to include files with specific names: panel.php for back-end route configuration and front.php for front-end route configuration.
Plugin Configuration Field Description
A simple fields.php is shown below:
return [
[
'name' => 'type', // Field name
'label' => 'Type', // With label_key, choose one, label represents the display name of the field
'label_key' => 'common.type', // With label, choose one, 'common.type' will call the key defined in the plugin's language pack to display the field name
'type' => 'select', // Field type, specific available types will be explained in the following sections
'options' => [
['value' => 'fixed', 'label_key' => 'common.fixed'],
['value' => 'percent', 'label_key' => 'common.percent'],
],
'required' => true, // Whether it is a required field
'rules' => 'required', // Field validation rules, refer to https://laravel.com/docs/11.x/validation#available-validation-rules
],
[
'name' => 'value',
'label_key' => 'common.value',
'type' => 'string',
'required' => true,
'rules' => 'required',
],
];
Configuration Field Type Description
Configuration Field Usage Instructions
Whether in the model (Model), view (View), or controller (Controller), you can use the plugin_setting('pluginName', 'configKey') function to get the plugin's configuration value.
Plugin Development Standards
Plugin Development Process
Taking the development of a PartnerLink plugin as an example:
Create a directory named PartnerLink in the /plugins directory, and create the required file config.json to define the basic information of the plugin. An example is as follows:
{
"code": "partner_link",
"name": {
"zh_cn": "友情链接",
"en": "Partner Links"
},
"description": {
"zh_cn": "友情链接",
"en": "Partner Links"
},
"type": "feature",
"version": "v1.0.0",
"icon": "/image/logo.png",
"author": {
"name": "InnoShop",
"email": "edward@innoshop.com"
}
}
Create the entry file Boot.php in the plugin root directory, and develop the plugin's controllers, models, views, etc., as needed. The Hook part can refer to the Hook Development Document. The entry file code is as follows:
namespace Plugin\PartnerLink;
use Plugin\PartnerLink\Models\PartnerLink;
class Boot
{
/**
* Method executed when the plugin is initialized.
*/
public function init(): void
{
/**
* Modify the left menu of the back-end management interface, add a menu item for the partner link.
* By listening to the system hook, add the partner link menu item to the back-end menu array.
*/
listen_hook_filter('component.sidebar.plugin.routes', function ($data) {
// Add partner link menu item
$data[] = [
'route' => 'partner_links.index', // Specify the route address
'title' => 'Partner Links', // The menu title displayed, support for multi-language definition. For example, use trans('PartnerLink::common.title'), the language resource package needs to be predefined in the Lang directory in advance
];
});
}
}
Define the corresponding code in the Migrations, Routes, Controllers, Models, Views directories.
If configuration is needed, define fields.php.
Plugin Activation and Deactivation
The activation and deactivation of plugins are carried out through InnoShop's back-end management system.
Conclusion
InnoShop's plugin mechanism provides developers with a flexible and highly extensible platform. Through this guide, developers can quickly master the basic process and standards of plugin development, contributing to InnoShop with a rich set of functional extensions.