Joomla MVC Component Class
Joomla! 4 has a new class for dispatching components. The class is called MVCComponent which extends Component class.
Component Class
This class implements ComponentInterface. It provides access to component specific services. The main purpose of this class is to create component dispatcher.
MVCComponent Class
This class implements MVCFactoryServiceInterface and use MVCFactoryServiceTrait. If you are not using any other service, you can use MVCComponent Class in your provider.php file. For example,
$component = new MVCComponent($container->get(ComponentDispatcherFactoryInterface::class));
$component->setMVCFactory($container->get(MVCFactoryInterface::class));
Extending MVCComponent Class
src/Extension/PlanetsComponent.php
If you want to use other services, you need to create your component class that extends MVCComponent. This file is located in the src/Extension folder at the admin part of the component.
For any service or integration you need to provide along with your component, this class will implement respective interface and use respective trait.
1. Router (Example: com_wrapper, com_tags)
- implements RouterServiceInterface
- use RouterServiceTrait
2. Custom Fields (Example: com_ursers)
- implements FieldsServiceInterface
3. Associations (Example: com_newsfeeds)
- implements AssociationServiceInterface
- use AssociationServiceTrait
4. Categories (Example: com_fields, com_newsfeeds, com_content)
- implements CategoryServiceInterface
- use CategoryServiceTrait
5. Tags (Example: com_newsfeeds, com_content)
- implements TagServiceInterface
- use TagServiceTrait
6. Workflows (Example: com_content)
- implements WorkflowServiceInterface
- use WorkflowServiceTrait
Register Services
services/provider.php
In the provider.php file, use your own component class instead of MVCComponent class.
$component = new PlanetsComponent($container->get(ComponentDispatcherFactoryInterface::class));
Then, register all the required services.
1. Router
$container->registerServiceProvider(new RouterFactory('\\TechFry\\Component\\Planets'));
$component->setRouterFactory($container->get(RouterFactoryInterface::class));