I managed to get it working. What was important for me was to create the API provider registration in a way that it only gets triggered when an API call is made, so the component MVC structure can stay the same. I only needed to create a model and JsonapiView for the needed CRUD calls.
Service Provider
return new class implements ServiceProviderInterface {
public function register(Container $container)
{
$container->registerServiceProvider(new MVCFactory('\\Joomla\\Component\\Mycomp'));
$container->registerServiceProvider(new ComponentDispatcherFactory('\\Joomla\\Component\\Mycomp'));
$container->set(
ComponentInterface::class,
static function (Container $container)
{
$component = new MycompComponent($container->get(ComponentDispatcherFactoryInterface::class));
// $component->setRouterFactory($container->get(RouterFactoryInterface::class));
return $component;
}
);
}
};
Component Class (src/Extensions/MycompComponent.php)
namespace Joomla\Component\Mycomp\Administrator\Extension;
use Joomla\CMS\Factory;
use Psr\Container\ContainerInterface;
use Joomla\CMS\Extension\ComponentInterface;
use Joomla\CMS\Dispatcher\DispatcherInterface;
use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Dispatcher\ComponentDispatcherFactoryInterface;
use Joomla\CMS\Dispatcher\LegacyComponentDispatcher;
\defined('JPATH_PLATFORM') or die;
class MycompComponent implements ComponentInterface {
private $dispatcherFactory;
public function __construct(ComponentDispatcherFactoryInterface $dispatcherFactory)
{
$this->dispatcherFactory = $dispatcherFactory;
}
public function getDispatcher(CMSApplicationInterface $application): DispatcherInterface
{
// use J4 Dispatcher only for API calls
if ($application->isClient('api')) {
return $this->dispatcherFactory->createDispatcher($application);
} else {
return new LegacyComponentDispatcher($application);
}
}
public function boot(ContainerInterface $container)
{
}
// no idea what this is for :)
public function validateSection($section, $item = null)
{
return;
}
public function getContexts(): array
{
Factory::getLanguage()->load('com_mycomp', JPATH_ADMINISTRATOR);
$contexts = [
];
return $contexts;
}
}