1

I am writing my first Joomla component. I found there are 3 so called entry points.

  1. installer
  2. site entry
  3. admin entry

I'd like to use some common utility classes by all of these entry points. These classes should be part of this component only, and should be installed in the package I make from this component.

What is the best practice to copy, register and load these classes? I found there is a JLoader, which you can use to register and load classes, but nothing more. It is not clear how to copy these classes, since their folder is on the same level as site and admin folders which are copied by install.

It is not clear either how to register these classes without adding any code to the entry points. Ofc. if that is not possible then I can live with that. I don't want to use PHP namespaces, since Joomla does not use them and I want to stay joomlish. :D

0

2 Answers 2

3

If the utility classes are only used in your component then include them in a folder within your administrator component. Whether you register the classes with JLoader or just require_once them just set the path like: JPATH_ADMINISTRATOR . '/your_component_name/utilities/a_class.php'. If you have several utility classes then put all calls to the classes in one file and then require_once that file from all 3 entry files.

To generalise the Joomla way is

  1. include all your code inside your component/module/plugin/template - custom code goes nowhere else.
  2. to avoid code duplication stick classes you use in the front end and the back end in the back end.
3
  • How should the manifest file look like? Should I copy the whole admin folder to the site as well?
    – inf3rno
    Feb 6, 2017 at 17:18
  • Presuming your component's manifest looks like the one at the bottom of this page: docs.joomla.org/J3.x:Developing_an_MVC_Component/Basic_backend Then just add a utilities folder (or whatever you wish to call it) to the administration > files list. Joomla does not mind if you have folders with names outside of the convention. So you utilities folder would be level with the backend controllers, models, views etc folders. I don't understand the last part of your question - why would you duplicate the admin folder in the frontend? Feb 6, 2017 at 21:20
  • I am trying out something different soon. I don't really need these site and admin folders. :-) Thanks for the help anyways!
    – inf3rno
    Feb 6, 2017 at 21:25
0

Since I don't want to dev two apps, like Joomla recommends, and I don't want to use MVC, I decided to put everything into a single folder under the same namespace.

The manifest file looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.4" method="upgrade">

    <name>example</name>
    <!-- ... -->

    <scriptfile>installer.php</scriptfile>

    <files>
        <filename>example.php</filename>
        <folder>views</folder>
        <folder>Example</folder>
        <folder>language</folder>
    </files>
    <!-- ... -->

    <administration>
        <menu>COM_EXAMPLE_TITLE</menu>
        <files>
            <filename>example.php</filename>
            <folder>Example</folder>
            <folder>language</folder>
        </files>
        <!-- ... -->
    </administration>

</extension>

My class declarations are in the Example folder and the classes are defined under the Example namespace.

I use the following code in the common example.php entrypoint.

JLoader::registerNamespace('Example', JPATH_COMPONENT);

use Example\Component;

Component::getInstance()->run();

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.