logo

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:

  • Controllers - Directory for controllers, including front-end and back-end controllers, optional.
  • Lang - Directory for language packs, consistent with the /resources/lang directory structure, optional.
  • Middleware - Directory for middleware, distinguishing between front-end and back-end middleware, optional.
  • Migrations - Directory for database migrations, optional.
  • Models - Directory for models, defining data table structures and relationships, optional.
  • Repositories - Directory for data repositories, containing data access classes, optional.
  • Routes - Directory for routes, containing the plugin's custom routes, optional.
  • Static - Directory for static files, such as images, optional.
  • Views - Directory for plugin templates, storing Blade template files, optional.
  • Boot.php - Plugin bootstrap class, containing the init method for adding hooks, optional.
  • fields.php - Plugin configuration file, defining plugin configuration fields, optional.
  • composer.json - Third-party composer packages that the plugin depends on, optional.
  • config.json - Basic information file for the plugin, including encoding, name, description, etc., required.

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:

php
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

  • bool - Boolean type, used to represent the state of a switch.
  • checkbox - Checkbox, suitable for multiple selection scenarios.
  • image - Image upload, saves the path of the uploaded image.
  • multi-rich-text - Multi-language rich text box, suitable for rich text editing in a multi-language environment.
  • multi-string - Multi-language string, suitable for simple text editing in a multi-language environment.
  • multi-textarea - Multi-language multi-line text box, suitable for multi-line text input in a multi-language environment.
  • rich-text - Rich text box, used for editing formatted text content.
  • select - Drop-down options, providing the function to choose a single option.
  • string - String, used for input and storage of short text information.
  • textarea - Multi-line text box, suitable for long text input.

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

  • Naming convention - Follow StudlyCaps to ensure automatic loading is correct.
  • Directory structure - Organize plugin files according to the above directory structure.
  • Configuration file - Use fields.php and config.json to define plugin configuration.
  • Template reference - Use the plugin_asset helper function to reference static resources.
  • Route definition - Define plugin routes in the Routes directory.

Plugin Development Process

Taking the development of a PartnerLink plugin as an example:

  1. Create the plugin directory

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:

json
{
    "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"
    }
}
  1. Write the code

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:

php
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.

  1. Configuration file

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.