Helpers

You are viewing documentation for an unreleased version of Craft CMS. Please be aware that the material is changing frequently and may be incomplete or inaccurate, and links may point back to older versions.

Most of Craft’s static “helper” classes have moved to the CraftCms\Cms\Support namespace.

#Functions

A handful of helper functions are defined in src/helpers.php, at the top level of the CraftCms\Cms namespace. These are mostly shortcuts for high-traffic features, like rendering templates (template()), translating strings (t()), or generating URLs (cp_url(), site_url()).

You are not obligated to use these helpers, but they provide a highly stable, public API that we hope will simplify and standardize common tasks.

#Facades

Laravel’s Facades (opens new window) architecture is a handy way to define your plugin’s public API. It also brings dependency injection (opens new window) support to a helper-like interface.

This facade wraps our Manager service class:

namespace MyOrg\Activity\Support;

use Illuminate\Support\Facades\Facade;

class Events extends Facade
{
    #[\Override]
    protected static function getFacadeAccessor(): string
    {
        return \MyOrg\Activity\Reporting\Manager::class;
    }
}

Facades can then be used as a shortcut, in lieu of reaching for the service container:

use MyOrg\Activity\Support\Events;

Events::track('pageView', ['elementId' => $entry->id]);

// This still works:
app(\MyOrg\Activity\Reporting\Manager::class)->track(
    'pageView',
    ['elementId' => $entry->id],
);

Either way, Laravel will make sure #[Singleton] service classes are reused.