# Plugin mechanism

# Principle

The BeikeShop plugin makes full use of the Laravel framework ServiceProvider discovery and boot mechanism to register various plugins in the system. Including the plugin's Migrations, Routes, Views, Lang, and the startup file Bootstrap.php.

Plugin PHP code needs to follow the PSR4 autoloading specification.

In short, directory names and class names need to use the first letter of the capital camel case, which is the Studly principle, as shown in the figure

img.png

# Catalog Description

A typical plugin directory is as follows

  • Controllers
  • Lang
  • Middleware
  • Migrations
  • Models
  • Repositories
  • Routes
  • Static
  • Views
  • Bootstrap.php
  • columns.php
  • config.json
  • README.md
Directory Name illustrate
Controllers Controller directory. Both the front-end and back-end controllers are placed in this directory. There can be sub-directories as long as they comply with the PSR4 specification.
Lang Language pack directory, the subdirectory name needs to be consistent with the /resources/lang directory
Middleware Middleware directory, subdirectory /Shop stores front-end middleware, subdirectory /Admin stores back-end middleware, the system will automatically load
Migrations Database migration directory, please refer to https://laravel.com/docs/9.x/migrations
Models Model directory, plug-in model, define model data tables and relationships between tables
Repositories Data warehouse directory, DB access related classes
Routes Routing directory, plugin custom routing. admin.php stores management backend routing, shop.php stores frontend routing
Static Static files, such as images, can be placed in the image directory and referenced in the corresponding plugin template file using the helper plugin_resize(plugin code, '/image/xx.png')
Views Plugin template directory, including front-end and back-end blade.php template files
Bootstrap.php The plugin startup class needs to implement a boot public method: public function boot(), and then add a hook to this method
columns.php Plugin configuration. This file stores the configuration fields required by the plugin, and the background plugin editing page will be automatically displayed. If you need a custom configuration page, please add the /Views/admin/config.blade.php template file under the plugin
config.json Basic information of the plugin, including code, name, description, type, icon, author, etc.
README.md Plugin usage documentation. The system will automatically read this file and display its content on the plugin editing page, making it easy for developers to provide operation guides and other content. This feature is available in beikeshop v3.0.0.4 and beikeshop v2.0.0.27 and above

# Plugin Core Architecture and Configuration

BeikeShop plugin directories must follow the PSR-4 specification. Pay special attention to the code field in config.json, which is the plugin's globally unique identifier. If the code conflicts with an existing plugin in the market, it will cause authorization verification to fail.

# config.json

Nehmen Sie als Beispiel das integrierte Plugin für die neueste Produktliste:

{
    "code": "latest_products",
    "name": {
        "zh_cn": "最新商品列表",
        "en": "Latest Products"
    },
    "description": {
        "zh_cn": "首页菜单添加最新商品列表功能",
        "en": "Latest products for header menu"
    },
    "type": "feature",
    "version": "v1.0.0",
    "icon": "/image/logo.png",
    "author": {
        "name": "成都光大网络科技有限公司",
        "email": "yangjin@guangda.work"
    }
}

Name und Beschreibung können nach obigem Format mehrsprachig konfiguriert werden. Die Sprachcodes lauten:

de,en,es,fr,id,it,ja,ko,ru,zh_cn,zh_hk

type Ist ein Plug-in-Typ, alle Typen haben:

Name Code
Payments payment
Marketing marketing
Shipping shipping
Theme theme
Analysis analysis
Service service
Languages language
Translator translator
Feature feature

# Plugin Logo Guidelines

The plugin marketplace automatically reads logo files from the Static/image directory inside the plugin package and uploads them when the developer uploads the ZIP package.

In most cases, you only need to provide one logo.png. logo_en.png is optional and is only needed if you want a separate logo for the English site.

Recommended directory structure:

YourPlugin/
├── Static/
│   └── image/
│       ├── logo.png
│       └── logo_en.png
├── config.json
└── Bootstrap.php

Rules:

  • logo.png: required, default logo. Recommended size: 500x500.
  • logo_en.png: optional, logo for the English site. Recommended size: 500x500.
  • If the plugin package only contains logo.png, the marketplace will use it for both zh_cn and en.
  • If the plugin package contains both logo.png and logo_en.png, the marketplace will store them separately for Chinese and English.
  • On the English site, the marketplace reads the en logo first. If it does not exist, it falls back to the zh_cn logo.

Recommendations:

  • Create logo.png directly in 500x500.
  • Use logo_en.png when your default logo contains Chinese text, so users on the .com site can see an English version.
  • If you provide both files, keep the same size and visual style, and only replace the text language in the image.
  • Use clear PNG images, and make sure the filenames are exactly logo.png and logo_en.png.

Notes:

  • "icon": "/image/logo.png" in config.json is still kept as the default icon path declaration.
  • In the marketplace, the displayed icon will prefer the uploaded multilingual logo data generated from the plugin package.

# Logic for Adding Features to Decoration Modules

The Hook mechanism can be used to inject plugin functionality into frontend templates or modify core data.

  • Frontend Template Hook (**add_hook_blade**): The first parameter here must correspond to the Blade Path in the Layout or view (e.g., header.top.telephone).
  • Data Flow Modification (**add_hook_filter**):

# Subscription-based Plugin Development Flow (Example: Discount Plugin)

Subscription plugins typically use a C/S architecture and verify authorization through a server-side API:

  1. Client-side Request: Use fetch or a PHP server-side request in the plugin logic for status validation.
  2. Listing Strategy: When publishing the plugin, you must check "Is Subscription" and, according to official requirements, set a free subscription period of no less than 1 month.