File Structure and Naming Conventions

From Joomla! Documentation

Other languages:
Deutsch • ‎English • ‎français • ‎中文(台灣)‎
Info non-talk.png
General Information

This document documents the new conventions specific to Joomla 4. Joomla 4 also supports the old Joomla 3 naming conventions for backwards compatibility purposes. If you want to support Joomla 3 and Joomla 4 at the same time you should continue to use the Joomla 3 conventions documented at File Structure and Naming Conventions for Joomla 3

Components in Joomla Core (and indeed generally in the Joomla ecosystem) follow the Model-View-Controller (MVC) design pattern. This pattern separates the data gathering (Model), presentation (View) and user interaction (Controller) activities of a component. Such separation allows for expanding or revising properties and methods of one section without requiring additional changes to the other sections. Please see the MVC tutorial, then select a Joomla version for an explanation of the MVC structure.

Joomla's file loading system enables developers to work with separate files for controllers, views and models without worrying about importing the right file in the right place. However, for this to work, certain naming conventions need to be observed.

The Joomla platform is extremely flexible, and it is possible to diverge from these conventions in many ways. It is nonetheless recommended to apply standard naming wherever possible to increase readability and maintainability. Additionally, the platform will take care of the many tedious class management tasks when the files and classes of a component are named correctly.

Reserved words[edit]

There are reserved words which can't be used in names of classes and components.

An example is the word "model" (in any case) for model classes (except "model" that must be second part of that class name)[1]. Because the first part of the name of model classes is the same as the controller class name, controller class names can't contain the word "model" either. And because of the conventions (although violating it won't produce an error), controller class names must contain the component name, so component names can't contain the word "Model" either. So components can't be named "com_modelling", or if they are, they must violate the naming conventions and have a different base controller class name (or have some other hacks).

Installation package vs. actual file placement[edit]

All Joomla extensions must be packaged as a .zip installation file. According to the most common file arrangement, the package should contain at least the admin folder in its basic structure. It may additionally contain (amongst others):

  • site
  • admin

If you set up your XML manifest file according to standard practice, the contents of site will be installed to /components/com_{componentname}, whereas the contents of admin will be installed to /administrator/components/com_{componentname} (N.B. In these paths the component name is lowercase).

The /site folder[edit]

This folder keeps the files for the frontend part of the component.

This folder holds the XML forms used by \Joomla\CMS\Form\Form.
This folder holds the views to be rendered in the template.
This folder holds the files for the view {ViewName} (note the path here uses the view name in lowercase).
This is the default template for the view {ViewName}. In this PHP file, the $this keyword refers to the view class that the template belongs to.
This folder holds the namespaced PHP code for the frontend of the component. For the conventions of what the namespaces will be please refer to Namespace Conventions in Joomla 4
This folder holds the different views for the component.
This file is the entry point for the view {ViewName}. This class must implement the interface \Joomla\CMS\MVC\View\ViewInterface and will extend from the format specific class (if core provides it - such as \Joomla\CMS\MVC\View\HtmlView) or from \Joomla\CMS\MVC\View\AbstractView. The filename is related to the format the view will be loaded in. For example, if the format URL parameter is set to format=feed, the file FeedView will be loaded.
This folder holds additional models, if needed by the application.
This file holds the model class {ModelName}. This class must extend the base class \Joomla\CMS\MVC\Model\BaseDatabaseModel. Note that the view named {ViewName} will by default load a model called {ViewName} if it exists. Most models are named after the view they are intended to be used with, but this is not a requirement. See Using multiple models in an MVC component for more information.
This folder holds additional controllers, if needed by the application.
This file holds the controller class {ControllerName}. This class must extend the base class \Joomla\CMS\MVC\Controller\BaseController.
This folder holds the site dispatcher.
This file holds the site dispatcher. It loads the language files, does a component access check and then gets and executes the controller, in components with HTML output also executing redirects if required.

The /admin folder[edit]

The file structure is almost exactly the same as in the /site folder. Note that the view, models, controllers etc. of the site and admin parts are by default completely separated, and have nothing to do with each other - the site part and the admin part can be thought of as two different components! A view in the /admin folder may have a counterpart with the same name in the /site folder, yet the two views have nothing in common but their class names (although they will have different namespaces to separate them).

There are two additional parts in the administrator directory:

This folder holds a single file.
This file holds an anonymous class implementing \Joomla\DI\ServiceProviderInterface. It's job is to return the Dependency Injection container provisioned with all the factories needed to run your component (in all 3 web applications they may run in).
This folder holds a single file.
This file is the public face of the extension, it retrieves data from the Container produced in the provider file above and exposes the relevant services (accessible via \Joomla\CMS\Application\CMSApplication::bootComponent()). This class is used by modules to get access to the component models or to run a component by Joomla when loading a page (by getting the dispatcher).

In some cases it might sometimes make sense to share classes between the site and admin parts of the component. Especially models can be shared to avoid duplicating model code. The Joomla platform supports this strategy. When sharing any code between site and admin applications, the classes should be designed with great care to avoid the possibility of a site user executing admin actions.

  1. Reason is in the getName function in file libraries/src/MVC/Model/BaseDatabaseModel.php: if (!preg_match('/Model(.*)/i', get_class($this), $r))