-
-
Notifications
You must be signed in to change notification settings - Fork 174
Component Structure and MVC Implementation
Joomla 5 follows the Model-View-Controller (MVC) design pattern, which separates data processing (Model), presentation (View), and user interaction (Controller). This separation enhances modularity, making it easier to extend and maintain components without affecting other sections.
Joomla's autoloading system allows developers to structure controllers, views, and models in separate files without manually including them. However, proper naming conventions must be followed to ensure smooth loading and execution.
Joomla uses {ComponentName} as the standard reference for a component (e.g., Content
). Case sensitivity is important:
-
{ComponentName}
→ CamelCase format -
{componentname}
→ Lowercase format -
{ViewName}
→ View name in CamelCase -
{viewname}
→ Lowercase view name -
{ModelName}
→ Model name in CamelCase -
{modelname}
→ Lowercase model name -
{ControllerName}
→ Controller name in CamelCase -
{controllername}
→ Lowercase controller name
Certain words cannot be used as part of class or component names. For example:
- The word "model" can only appear as the second part of a model class name (e.g.,
ContentModelArticles
). - Component names cannot contain "Model," as controllers inherit naming conventions from models.
All Joomla extensions must be packaged as a .zip
file and contain at least the following directories:
com_{componentname}/
├── site/
└── admin/
-
site/
: Holds frontend files. -
admin/
: Holds backend (administrator) files.
When installed:
-
site/
is placed under/components/com_{componentname}
-
admin/
is placed under/administrator/components/com_{componentname}
-
site/{componentname}.php
→ The component's main entry file.
-
site/controller.php
→ Default controller. - Defines
{ComponentName}Controller
, extending\Joomla\CMS\MVC\Controller\BaseController
. - Additional controllers go under
site/controllers/{controllername}.php
.
-
site/views/{viewname}/view.html.php
→ Main view file. - Declares
{ComponentName}View{ViewName}
extending\Joomla\CMS\MVC\View\HtmlView
. - Templates for the view reside in:
site/views/{viewname}/tmpl/ ├── default.php (Main template file)
-
site/models/{modelname}.php
→ Model class. - Declares
{ComponentName}Model{ModelName}
extending\Joomla\CMS\MVC\Model\BaseDatabaseModel
. - By default, a view loads a model with the same name (if it exists).
The /admin
directory follows the same structure as /site
, but it is entirely separate.
Key points:
- Admin and site components are distinct; sharing code is optional.
- Shared models can reduce code duplication.
- To avoid security risks, ensure site users cannot execute admin actions.
Joomla supports shared models and utility classes between frontend and backend. When sharing code:
- Place shared classes in a neutral directory (
/models
,/helpers
). - Ensure class design prevents unauthorized access to admin functionalities.
Joomla enforces naming rules via the getName()
function in:
libraries/src/MVC/Model/BaseDatabaseModel.php
:
if (!preg_match('/Model(.*)/i', get_class($this), $r)) {
throw new RuntimeException("Invalid model class name");
}
This ensures proper MVC structure is maintained in Joomla 5.
By adhering to Joomla's MVC architecture and naming conventions, developers can build maintainable, extensible, and well-structured components. Keeping site and admin logic modular improves reusability and security within the Joomla ecosystem.
- Home
- Beta Testing
- Custom Code
- PHP Settings
- Demo Component
-
Tutorials
- Hello World JCB
- Intro JCB Guide
- JCB Installation Steps
- Planning Components
- Field Type Overview
- Basic Fields Creation
- Admin View Management
- Advanced Field Usage
- Admin Component Integration
- Component Setting Customization
- Scripting Components
- Component FTP Options
- Dynamic Get Method
- Site View DynamicGet
- Site View Templates
- Template Setup Guide
- Layout Configuration Steps
- Custom Admin Management
- Adding Site Views
- Custom Admin Integration
- MySQL Demo Tweaking
- Global JCB Settings
- Custom Time Field
- User Helper Integration
- Email Helper Usage
- Message Store Email
- List View Unescape
- Export Import Customization
- Overwrite Custom Fields
- List Field Filtering
- Automatic Code Import
- Manual Code Implementation
- Component Export Import
- Custom Admin Buttons
- Translation Management
- Site View Permissions
- Component SQL Updates
- Site Edit Configuration
- JCB Backup System
- Helper Structure Integration
- JCB v2.5 Upgrade
- Tab Setup Guide
- JCB v2.6 Release
- Extended HelloWorld
- Field Rule Validation
- Community Snippets Intro
- Snippet Forking Tutorial
- Pull Request Snippets
- Library Manager Area
- Excel-based Translation
- Dynamic Router Details
- Database Auto Updates
- Subform Quick Demo
- VDM Package Import
- Dynamic File Inclusion
- File Field Upload
- Drag-n-Drop Upload
- Quick HelloWorld JCB
- Non-database Fields
- Dashboard Customization
- Menu Prefix Toggle
- Community JCB Packages
- Collaborative JCB Workflow
- JCB Package Install
- JCB JAB18 Event
- Convenient New Fields
- Component Language Strings
- Library Functionality Anticipation
- Join Field Relations
- License Template Change
- Code Reusability
- Local Dev Environment
- Extended Field Types
- Joomla Custom Fields
- Custom Field Expansion
- Site View Listing
- Run Expansion Method
- Form Site View
- Field URL Update
- Additional Helper Methods
- Field Validation Rules
- New Placeholder Feature
- Component Config Params
- Per-field Default Values