Moko Consulting

Open-source software for Joomla, Gitea, and web platforms. Home of MokoSuite, MokoGitea, and MokoCLI.

Tennessee
api/joomla-webservices.-

Joomla Web Services

How to create Web Services plugins for MokoSuite components.

Plugin Structure

plg_webservices_mokosuitebackup/
├── mokosuitebackup.xml
├── mokosuitebackup.php         # Empty legacy entry
├── services/provider.php       # DI registration
├── src/Extension/
│   └── MokoSuiteBackupWebServices.php
└── language/en-GB/
    ├── plg_webservices_mokosuitebackup.ini
    └── plg_webservices_mokosuitebackup.sys.ini

Route Registration

namespace MokoConsulting\Plugin\WebServices\MokoSuiteBackup\Extension;

use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\SubscriberInterface;
use Joomla\Router\Route;

final class MokoSuiteBackupWebServices extends CMSPlugin implements SubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return ['onBeforeApiRoute' => 'registerRoutes'];
    }

    public function registerRoutes(\Joomla\Event\Event $event): void
    {
        $router = $event->getArgument('router');

        // Standard CRUD routes
        $router->createCRUDRoutes(
            'v1/mokosuitebackup/backups',
            'backups',
            ['component' => 'com_mokosuitebackup']
        );

        $router->createCRUDRoutes(
            'v1/mokosuitebackup/profiles',
            'profiles',
            ['component' => 'com_mokosuitebackup']
        );

        // Custom action routes
        $router->addRoute(new Route(
            ['POST'],
            'v1/mokosuitebackup/backup',
            'backup.run',
            [],
            ['component' => 'com_mokosuitebackup', 'public' => false]
        ));
    }
}

How It Works

  1. Joomla dispatches the onBeforeApiRoute event on API requests
  2. The webservices plugin registers routes mapping URLs to controller tasks
  3. createCRUDRoutes() auto-generates GET list, GET single, POST, PATCH, DELETE routes
  4. Routes map to the component's API controllers (in api/src/Controller/)
  5. Controllers extend Joomla\CMS\MVC\Controller\ApiController
  6. Response is automatically serialized to JSON:API format

API Controller

namespace MokoConsulting\Component\MokoSuiteBackup\Api\Controller;

use Joomla\CMS\MVC\Controller\ApiController;

final class BackupsController extends ApiController
{
    protected $contentType = 'backups';
    protected $default_view = 'backups';

    protected function allowAdd(array $data = []): bool
    {
        return $this->app->getIdentity()->authorise('core.create', 'com_mokosuitebackup');
    }

    protected function allowEdit(array $data = [], $key = 'id'): bool
    {
        return $this->app->getIdentity()->authorise('core.edit', 'com_mokosuitebackup');
    }
}

Testing the API

# List backups
curl -s "https://site.com/api/index.php/v1/mokosuitebackup/backups" \
  -H "Authorization: Bearer {token}" \
  -H "Accept: application/vnd.api+json"

# Create a profile
curl -s -X POST "https://site.com/api/index.php/v1/mokosuitebackup/profiles" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{"title": "Daily Full Backup", "backup_type": "full"}'