Sapphire Documentation

Plugins

Sapphire provides modern plugins system. To add plugin to Sapphire just put it inside resources/plugins/ folder.


Plugin conists of 2 main components:

  • Plugin entry
  • Plugin config (json)


Plugin.json

{
    "name": "PluginName",
    "config": {}
}


Plugin.entry.php

<?php
    use Sapphire\Plugin\Plugin;
    use Sapphire\Plugin\Helpers\Sidebar;
    use Sapphire\Plugin\Helpers\Router;
    use Sapphire\Plugin\Helpers\AssetInjector;

    class PluginName extends Plugin {
        use Sidebar;
        use Router;
        use AssetInjector;

        public function Init(): void {
            /////////////////////////////////////////
            // Register required events
            /////////////////////////////////////////
            $this->RegisterEvents([
                "eventName" => [$this, "Callback"],
                "onAdminHead" => [$this, "HandleGenerateStart"]
            ]);

            /////////////////////////////////////////
            // Add route
            /////////////////////////////////////////
            $this->AddRoute(path: "/admin/plugin-view-123", view: "PluginView", data: [ "plugin" => $this ]);
        } 


        public function HandleGenerateStart(): void {
            $this->InjectScript(__DIR__ . "/dist/index.js");
            $this->InjectStyle(__DIR__ . "/dist/index.css");

            /////////////////////////////////////////
            // Create sidebar button
            /////////////////////////////////////////
            $this->CreateSidebarButton(
                name: "Plugin sidebar button",
                icon: "fas fa-sun",
                link: "/admin/plugin-view-123"
            );
        }
    }

    return PluginName::class;


Plugin views

Plugins views are located in plugin folder inside views subfolder.


Plugin includes

Inside plugin folder there is another folder includes all of files placed there will be automatically included to use in Plugin entry.