1

I'm rebuilding a component called DogWalk for Joomla 4 and I've tried to ensure I'm doing exactly what the core Banners component does. I've named and namespaced my files, named my directories identically to how Banners does it, but I can't get the BaseController to find my view file.

I'm dumping the $this->paths array from BaseController::createView, and the path is the same for Banners and for DogWalk:

array (size=1)
  'view' => 
    array (size=1)
      0 => string '/Users/mystuff/Sites/joomla4/administrator/components/com_dogwalk/views/'

However my component is looking for a file called mycomponentViewviewname while the actual view class name for Banners (and all the other admin components) is just HtmlView. Furthermore the Banners view class is found even though it's in View, not views (despite what $config array says).

How do I define where the controller will find my views, and what naming convention they would use?

5
  • Is your component properly namespaced? Is its record present in libraries/autoload_psr4.php file?
    – Sharky
    Dec 6, 2019 at 6:26
  • You know that you really do not have to define any location for the view since the controller automatically finds it. You see the createView() and getView() methods of the BaseController, then you of course know how it finds the view, if you set your component correctly. I do not really see any good answer on your question here, since most probably you'll will find a typo as usually these questions end up...
    – Zollie
    Dec 6, 2019 at 9:46
  • @Sharky there is no line for my component in the libraries/vendor/composer/autoload_psr4.php file-- should that be automatically reloaded when you install a component?
    – user101289
    Dec 9, 2019 at 3:16
  • You are looking at the wrong file. The file I mentioned, libraries/autoload_psr4.php, should get updated when installing extensions if Extension - Namespace Updater plugin is enabled.
    – Sharky
    Dec 9, 2019 at 6:21
  • @Sharky thanks, I verified that the plugin was working as expected and had added the namespace in the libraries/autload_psr4.php. Still not loading the files, but at least we can rule that out.
    – user101289
    Dec 10, 2019 at 2:37

2 Answers 2

1

The view file that your component is looking for MycomponentViewViewname which was used in the Joomla 3, so make sure you are extending the right controller class in your Controller(i.e Joomla\CMS\MVC\Controller\BaseController or any other class extending it such as AdminController). If it is from correct namespace track the setView() for any such changes that can cause the problem.

If this doesn't solve your problem then debugging the whole flow will be the option. But before that, I will suggest running the extension on some other fresh Joomla 4 setup on default parameters(as maybe some changes are affecting it such as code changes or plugin overrides).

If you are unable to find any solution you can override the setView() method in your controller as per your need(this should be the last option).

Hope this might help. Do let us know the solution once you find it.

0

Thanks to all who helped. The issue appears to have been the services file, not my pathing or namespacing. Once I added a file in administrator/services/provider.php it all started to work automagically. The file looked like this:

return new class implements ServiceProviderInterface
{
    /**
     * Registers the service provider with a DI container.
     *
     * @param   Container  $container  The DI container.
     *
     * @return  void
     *
     * @since   4.0.0
     */
    public function register(Container $container)
    {
        $container->registerServiceProvider(new MVCFactory('\\Mycompany\\Component\\Mycomponent'));
        $container->registerServiceProvider(new ComponentDispatcherFactory('\\Mycompany\\Component\\Mycomponent'));

        $container->set(
            ComponentInterface::class,
            function (Container $container)
            {
                $component = new CategoriesComponent($container->get(ComponentDispatcherFactoryInterface::class));

                $component->setMVCFactory($container->get(MVCFactoryInterface::class));
                $component->setRegistry($container->get(Registry::class));

                return $component;
            }
        );
    }
};

Another thing I needed to add was the generic DisplayController in both the admin and site Controller directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.