I'm developing a component that is targeting Joomla 3.9+. The code is not using the legacy classes and is instead using the new namespaces. The component will duplicate much of the functionality from the admin side so I'd prefer to share code and Sql queries where applicable. What is the best way to do this moving forward? Should I use the admin model on the site? Should I create a library or namespace? What is the preferred way to do this with the approaching 4.0 release? Also, is there a good way to share ui elements and forms between the two?
Should I use the admin model on the site?
That's what I've done in the past. Historically I've used admin models on the site side of things by doing something like this:
$controller->addModelPath(JPATH_COMPONENT_ADMINISTRATOR . '/models');
These days the
JLoader supports PSR-4 style namespaces. If I were to build a component from scratch today, that's what I would use. Something along the lines of:
JLoader::registerNamespace('App', JPATH_COMPONENT_SITE, false, false, 'psr4'); JLoader::registerNamespace('App\\Admin', JPATH_COMPONENT_ADMINISTRATOR, false, false, 'psr4');
I would probably also lean towards keeping shared assets in the
site side of things.