Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Component: structure and entry point

A component is the largest Joomla extension: a section with its own pages, database tables and control panel screens. We start the com_hello component — a simple blog with posts. This lesson creates its skeleton: the manifest and the provider. Pages appear in the next one.

Two folders

A component has two parts, and they live in different places of the site:

administrator/components/com_hello/     the control panel part
├── hello.xml                           the manifest
├── services/
│   └── provider.php                    the entry point — one for both parts
├── src/                                panel classes
└── tmpl/                               panel layouts
 
components/com_hello/                   the site part
├── src/                                site classes
└── tmpl/                               site layouts

Create these folders by hand. The manifest and the provider always live in the administrator part — even if the component works only on the site.

The manifest

<?xml version="1.0" encoding="UTF-8"?>
<!-- administrator/components/com_hello/hello.xml -->
<extension type="component" method="upgrade">
    <name>com_hello</name>
    <version>1.0.0</version>
    <description>A learning blog</description>
    <namespace path="src">My\Component\Hello</namespace>
 
    <files folder="site">
        <folder>src</folder>
        <folder>tmpl</folder>
    </files>
 
    <administration>
        <menu>Hello</menu>
 
        <files folder="administrator">
            <folder>services</folder>
            <folder>src</folder>
            <folder>tmpl</folder>
        </files>
    </administration>
</extension>
Manifest partPurpose
The first files sectionFiles of the site part
The administration sectionEverything that belongs to the panel
menuThe item name in the Components menu of the panel
The folder attributesFolders in the ZIP archive the installer takes files from

Namespace

The manifest has one namespace, and Joomla splits it into parts itself:

NamespaceFolder
My\Component\Hello\Sitecomponents/com_hello/src
My\Component\Hello\Administratoradministrator/components/com_hello/src
My\Component\Hello\Apiapi/components/com_hello/src

The provider

// administrator/components/com_hello/services/provider.php
defined('_JEXEC') or die;
 
use Joomla\CMS\Dispatcher\ComponentDispatcherFactoryInterface;
use Joomla\CMS\Extension\ComponentInterface;
use Joomla\CMS\Extension\MVCComponent;
use Joomla\CMS\Extension\Service\Provider\ComponentDispatcherFactory;
use Joomla\CMS\Extension\Service\Provider\MVCFactory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
 
return new class () implements ServiceProviderInterface {
    public function register(Container $container): void
    {
        $container->registerServiceProvider(new MVCFactory('\\My\\Component\\Hello'));
        $container->registerServiceProvider(new ComponentDispatcherFactory('\\My\\Component\\Hello'));
 
        $container->set(
            ComponentInterface::class,
            function (Container $container) {
                $component = new MVCComponent($container->get(ComponentDispatcherFactoryInterface::class));
                $component->setMVCFactory($container->get(MVCFactoryInterface::class));
 
                return $component;
            },
        );
    }
};

When a request with option=com_hello arrives, Joomla runs this file. MVCFactory knows how to create the component's controllers, models and views by name, and MVCComponent is a ready-made class for a component built on the MVC pattern.

The MVC pattern

PartFolder in srcWhat it does
ControllerControllerReceives the request and decides what to do
ModelModelFetches and saves data
ViewViewTakes data from the model and passes it to the layout
Layouttmpl (next to src)Outputs HTML

Installing

Open System → Install → Discover, press Discover and install com_hello. The Components menu of the panel gets a "Hello" item. For now it leads to an error page: the component has no controllers yet. The next lesson fixes that.

Common mistake

The manifest is placed in components/com_hello — by analogy with a module. Discover does not find such a component: Joomla looks for a component manifest only in administrator/components/com_hello. The services folder must be there as well.

Thanks for reading!

Donate to the site — we work hard for you.