Skip to main content

Helper Classes & Static Utilities

Extend your components with built-in, reusable logic โ€” ready to call from anywhere in your codebase.

What Are Helper Classes in JCB?

Helper Classes in Joomla Component Builder (JCB) are reusable PHP classes automatically generated with every component you build. They contain utility methods, shortcuts, and logic that can be used throughout your component โ€” in models, controllers, layouts, templates, or custom code blocks.

These helpers are especially useful for common tasks like fetching users, formatting data, building URLs, loading configurations, and accessing your own custom logic. JCB allows you to inject your own helper methods or use the built-in ones provided out of the box.

Every compiled component includes a main helper class (e.g., com_mycomponentHelper), and optionally static helper sub-classes that can be extended or customized.

Key Features

  • ๐Ÿงฐโ€‚Auto-Generated Helper Class
    Each component comes with a base helper (e.g., MyComponentHelper.php), included and namespaced properly.
  • ๐Ÿ“ฆโ€‚Preloaded Static Utility Methods
    Includes common methods like:
    • getCurrentUser()
    • getComponentParams()
    • getReturnPage()
    • checkAccess()
    • formatDate(), formatCurrency()
  • ๐Ÿง โ€‚Joomla Integration
    Built to interact with Joomla's core using version-aware logic (e.g., JFactory, JUri).
  • ๐Ÿ”„โ€‚Reusable in MVC, Layouts, and Snippets
    Call helper methods from models, controllers, layouts, templates, or snippets.
  • ๐Ÿงชโ€‚Extendable with Super Powers
    Link custom interfaces, traits, or abstract logic for DRY principles and inheritance.
  • ๐Ÿช„โ€‚Works with Placeholders
    Use {helper:myMethod()} โ€” JCB auto-converts to static calls.
  • ๐Ÿ’พโ€‚Located in helpers/ directory
    Compiled and versioned for easy IDE indexing.

Use Cases

  • ๐Ÿ”„ Convert date/time formats in layout display using formatDate()
  • ๐Ÿ” Validate permissions for specific actions with checkAccess()
  • ๐Ÿ“ฆ Fetch a field value from config using getComponentParams()
  • ๐Ÿ” Share logic across views by defining reusable helper methods
  • ๐Ÿงฉ Use in snippets via {helper:yourMethod(params)}

Example Usage

In a layout file:

<?php echo \Joomla\Component\Mycomponent\Site\Helper\MycomponentHelper::formatDate($item->created); ?>

Or in a template/snippet with placeholder:

{helper:formatDate($item->created)}

How to Add Custom Helper Methods

  1. In JCB, go to the Custom Code section for the Helper class.
  2. Add your method using full PHP syntax (Insert Tags supported).
  3. JCB injects the method into the helper.php file during compile.
  4. Use it in layouts, views, models, or snippets.

๐Ÿง  Pro Tip: Use naming patterns like getSomething(), isSomething(), or formatSomething() for clean, IDE-friendly code.

Conclusion

Helper Classes in JCB empower you to write clean, centralized logic and call it from anywhere in your component. They promote reusability, simplify maintenance, and make your codebase more organized โ€” especially in large projects with lots of repeated logic.