nikosdion / joomla_extensions_development

Joomla Extensions Development: a reference book
GNU Free Documentation License v1.3
10 stars 1 forks source link

Component namespace MUST contain "\Component\", followed by the extension name #24

Closed HermanPeeren closed 1 year ago

HermanPeeren commented 1 year ago

In https://www.dionysopoulos.me/book/com-namespaces-j4.html a namespace for components is suggested without a "\Component\" part. This however leads to a wrong component name in ComponentHelper::getComponentName(), see https://github.com/joomla/joomla-cms/blob/4.2-dev/libraries/src/Component/ComponentHelper.php lines 447-450:


        $from = strpos($reflect->getNamespaceName(), '\\Component');
        $to   = strpos(substr($reflect->getNamespaceName(), $from + 11), '\\');

        return 'com_' . strtolower(substr($reflect->getNamespaceName(), $from + 11, $to));

The example namespace "\Acme\Example" would result in a component name: com_e. A call to getComponentName() with this result is for instance used when adding a new item in a backend list view, reuslting in a 404.

Solution: Component namespace MUST contain "\Component\", followed by the extension name. The namespace in the example should be: "\Acme\Component\Example".

I will make a PR to correct this.