Custom component without MVC

For Joomla! 4.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
User avatar
ies
Joomla! Apprentice
Joomla! Apprentice
Posts: 42
Joined: Mon Mar 21, 2011 12:38 pm
Contact:

Custom component without MVC

Post by ies » Tue Jan 25, 2022 10:39 am

Hi,

Is there any documentation about how to create a custom component for Joomla 4 without using the MVC model?
Something like this tutorial https://docs.joomla.org/J4.x:Developing ... _Component but without the folders for src and tmpl.

Thanks

User avatar
ceford
Joomla! Guru
Joomla! Guru
Posts: 711
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Custom component without MVC

Post by ceford » Wed Jan 26, 2022 1:20 am

ies wrote:
Tue Jan 25, 2022 10:39 am
Is there any documentation about how to create a custom component for Joomla 4 without using the MVC model?
Something like this tutorial https://docs.joomla.org/J4.x:Developing ... _Component but without the folders for src and tmpl.
No! And if there were I would not tell you. A Joomla 4 component should be built using Joomla 4 conventions, for which there are all sorts of good reasons. For example, toolbar buttons in the Admin interface use links of the form controller.task from which Joomla looks for a public function in a controller in the src/Controller folder.

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 41252
Joined: Sat Apr 05, 2008 9:58 pm

Re: Custom component without MVC

Post by Webdongle » Wed Jan 26, 2022 2:00 am

+1 @ceford
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"When I'm right no one remembers but when I'm wrong no one forgets".

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2121
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Custom component without MVC

Post by SharkyKZ » Wed Jan 26, 2022 8:18 am

There is no official documentation. But actually a lot of work in J4 went into allowing better customization of component structure. It doesn't rely on file structure as much anymore, except for the service provider file (services/provider.php).

As a bare minimum, you need the service provider to provide a component instance (an implementation of Joomla\CMS\Extension\ComponentInterface). This in turn needs to return a dispatcher (an implementation of Joomla\CMS\Dispatcher\DispatcherInterface).

Component class, besides providing the dispatcher, defines supported features like routing, tagging, workflows and so on. To enable these features, have it implement appropriate interfaces.

The dispatcher acts as a front controller for your component and is the point of interest to you. Here you decide how the component is executed.

User avatar
ies
Joomla! Apprentice
Joomla! Apprentice
Posts: 42
Joined: Mon Mar 21, 2011 12:38 pm
Contact:

Re: Custom component without MVC

Post by ies » Wed Jan 26, 2022 3:02 pm

Thank you for the answers. I am sorry but, despite I tried many times, I am not fan of MVC. I have my own way to organize the code.

SharkyKZ, I was very close to your idea, but another guy (Hannes Papenberg) said that I can also avoid provider.php by deleting the whole folder of services and using the default entry php file that we were using in J3. So this is even better for me.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2121
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Custom component without MVC

Post by SharkyKZ » Wed Jan 26, 2022 3:31 pm

Okay, but this should stop working eventually, possibly in J5. It also requires to follow certain naming conventions for helper files used by core, as it was in J3. So it's not entirely free.

User avatar
ceford
Joomla! Guru
Joomla! Guru
Posts: 711
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Custom component without MVC

Post by ceford » Wed Jan 26, 2022 3:40 pm

ies wrote:
Wed Jan 26, 2022 3:02 pm
Thank you for the answers. I am sorry but, despite I tried many times, I am not fan of MVC. I have my own way to organize the code.
I would be interested to know what you are building. I have often thought the distinction between Model and Controller is a bit fuzzy and I admit to updating a database record from a controller where only one field needs to change. There was an article recently that implied that is bad practice.

User avatar
ies
Joomla! Apprentice
Joomla! Apprentice
Posts: 42
Joined: Mon Mar 21, 2011 12:38 pm
Contact:

Re: Custom component without MVC

Post by ies » Wed Jan 26, 2022 4:21 pm

@SharkyKZ
That is a good point. I believe that if it is going to stop working, it will happen in J5, so I will use it for now.

@ceford
My first component was using the MVC model. I was not satisfied. It was like someone else was programming for me :D
After that, I created some good/big components without MVC and they keep working very well and the biggest satisfaction was that I could arrange the code as I wanted.
Of course, I continue to use the other Joomla functions for url routing, text translation, sql queries, user access etc. It is only the MVC that I don't like.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2121
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Custom component without MVC

Post by SharkyKZ » Mon Jan 31, 2022 6:54 am

Anyways, if you want to play around with it, this is what the bare minimum (but with J4 native autoloading) component would look like:

Service provider (services/provider.php):

Code: Select all

\defined('_JEXEC') || exit;

use Joomla\CMS\Extension\ComponentInterface;
use Acme\Component\Barebone\Administrator\BareboneComponent;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

return new class implements ServiceProviderInterface
{
	public function register(Container $container): void
	{
		$container->set(
			ComponentInterface::class,
			static fn () => new BareboneComponent
		);
	}
};
Component class:

Code: Select all

namespace Acme\Component\Barebone\Administrator;

\defined('_JEXEC') || exit;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Dispatcher\DispatcherInterface;
use Joomla\CMS\Extension\ComponentInterface;

class BareboneComponent implements ComponentInterface
{
	public function getDispatcher(CMSApplicationInterface $application): DispatcherInterface
	{
		return new BareboneDispatcher;
	}
}
Dispatcher class:

Code: Select all


namespace Acme\Component\Barebone\Administrator;

\defined('_JEXEC') || exit;

use Joomla\CMS\Dispatcher\DispatcherInterface;

class BareboneDispatcher implements DispatcherInterface
{
	public function dispatch()
	{
		// This is where component is executed.
	}
}

TruthSword
Joomla! Apprentice
Joomla! Apprentice
Posts: 44
Joined: Sat Feb 15, 2020 12:48 am

Re: Custom component without MVC

Post by TruthSword » Wed Apr 27, 2022 5:58 am

SharkyKZ wrote:
Mon Jan 31, 2022 6:54 am
Anyways, if you want to play around with it, this is what the bare minimum (but with J4 native autoloading) component would look like:

Service provider (services/provider.php):

Code: Select all

\defined('_JEXEC') || exit;

use Joomla\CMS\Extension\ComponentInterface;
use Acme\Component\Barebone\Administrator\BareboneComponent;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

return new class implements ServiceProviderInterface
{
	public function register(Container $container): void
	{
		$container->set(
			ComponentInterface::class,
			static fn () => new BareboneComponent
		);
	}
};
Component class:

Code: Select all

namespace Acme\Component\Barebone\Administrator;

\defined('_JEXEC') || exit;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Dispatcher\DispatcherInterface;
use Joomla\CMS\Extension\ComponentInterface;

class BareboneComponent implements ComponentInterface
{
	public function getDispatcher(CMSApplicationInterface $application): DispatcherInterface
	{
		return new BareboneDispatcher;
	}
}
Dispatcher class:

Code: Select all


namespace Acme\Component\Barebone\Administrator;

\defined('_JEXEC') || exit;

use Joomla\CMS\Dispatcher\DispatcherInterface;

class BareboneDispatcher implements DispatcherInterface
{
	public function dispatch()
	{
		// This is where component is executed.
	}
}
SharkyKZ,

Are all three code segments here for the 1 file: Service provider (services/provider.php): ? the "Service provider (services/provider.php):" block + 2 classes?

Do you have a github with this basic j4x component w/ file structure?

Thank you very much!

Best Regards,

Brandon

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2121
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Custom component without MVC

Post by SharkyKZ » Wed Apr 27, 2022 6:49 am

This example follows Joomla's native PSR-4 autoloading so these are separate files. You can read about PSR-4 here https://www.php-fig.org/psr/psr-4/. In your thread I have linked to a version that uses anonymous classes, entirely contained in the single services/provider.php file. I'd stick with concrete classes for maintainability though.

Some things specific to Joomla components regarding PSR-4 autoloading:

1. The base namespace is declared in the manifest XML file using <namespace> tag. It supports an optional path attribute which allows placing the class files in a subdirectory, relative to component directory. I do use a subdirectory to keep classes separated from the rest of the files. This is the tag used in the example component:

Code: Select all

<namespace path="src">Acme\Component\Barebone
2. Joomla appends application name to your base namespace based on in which application the classes are placed. E.g. if your classes are in the admin part (administrator/components/com_barebone), their base namespace would be Acme\Component\Barebone\Administrator. Classes in site part (components/com_barebone) would have Acme\Component\Barebone\Site base namespace.

So for, instance, my class in administrator/components/com_barebone/src/Mvc/Model/ItemModel.php would have this name:

Code: Select all

Acme\Component\Barebone\Administrator\Mvc\Model\ItemModel
Once these basics are met, it's up to your how to organize the classes. You can place all your classes (i.e. both administrator and site) in just the administrator or just the site folder, if you want (only services/provider.php has hard requirement to be in administrator part). You can use as many subnamespaces as you want or even completely flat structure. As long as the base namespace is correct and the PSR-4 class name/file location convention is followed.

I don't have this on Github but I do have an installable package. See attachment.
You do not have the required permissions to view the files attached to this post.

TruthSword
Joomla! Apprentice
Joomla! Apprentice
Posts: 44
Joined: Sat Feb 15, 2020 12:48 am

Re: Custom component without MVC

Post by TruthSword » Sat Apr 30, 2022 5:01 am

SharkyKZ wrote:
Wed Apr 27, 2022 6:49 am
This example follows Joomla's native PSR-4 autoloading so these are separate files. You can read about PSR-4 here https://www.php-fig.org/psr/psr-4/. In your thread I have linked to a version that uses anonymous classes, entirely contained in the single services/provider.php file. I'd stick with concrete classes for maintainability though.

Some things specific to Joomla components regarding PSR-4 autoloading:

1. The base namespace is declared in the manifest XML file using <namespace> tag. It supports an optional path attribute which allows placing the class files in a subdirectory, relative to component directory. I do use a subdirectory to keep classes separated from the rest of the files. This is the tag used in the example component:

Code: Select all

<namespace path="src">Acme\Component\Barebone
2. Joomla appends application name to your base namespace based on in which application the classes are placed. E.g. if your classes are in the admin part (administrator/components/com_barebone), their base namespace would be Acme\Component\Barebone\Administrator. Classes in site part (components/com_barebone) would have Acme\Component\Barebone\Site base namespace.

So for, instance, my class in administrator/components/com_barebone/src/Mvc/Model/ItemModel.php would have this name:

Code: Select all

Acme\Component\Barebone\Administrator\Mvc\Model\ItemModel
Once these basics are met, it's up to your how to organize the classes. You can place all your classes (i.e. both administrator and site) in just the administrator or just the site folder, if you want (only services/provider.php has hard requirement to be in administrator part). You can use as many subnamespaces as you want or even completely flat structure. As long as the base namespace is correct and the PSR-4 class name/file location convention is followed.

I don't have this on Github but I do have an installable package. See attachment.

SharkyKZ,

Thank you very much for this write up and zip attachment share! I see that I have a lot to learn. I truly appreciate this!

:)

Best Regards,

Brandon :D

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 28816
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Custom component without MVC

Post by Per Yngve Berg » Sat Apr 30, 2022 11:24 am

Without MVC, the template developer will not be able to override the output to suit the template.

TruthSword
Joomla! Apprentice
Joomla! Apprentice
Posts: 44
Joined: Sat Feb 15, 2020 12:48 am

Re: Custom component without MVC

Post by TruthSword » Sat Apr 30, 2022 2:54 pm

Per Yngve Berg wrote:
Sat Apr 30, 2022 11:24 am
Without MVC, the template developer will not be able to override the output to suit the template.
Per Yngve Berg,

That's good to know! Thank you for the details on this. If I choose that route; I will learn to design the template myself.


Post Reply

Return to “Joomla! 4.x Coding”