Service Container

Oct 21, 2016

In good old v5.6 days, we used the Loader class and its static methods (Loader::db(), Loader::helper(), Loader::element(), …) to get instances of database connections, helpers, and so on.

With v5.7 (and even more with v8) we switched from this approach to Laravel Service Containers (for a more detailed explanation of them you can read the Laravel manual).

In an effort to simplify the upgrade of existing code from v5.6 to v5.7, we introduced some Facades (for instance: Core::make(), Database::get(), Config::get(), …). Facades are very handy, but their performance is sub-optimal since they introduce a few slow PHP calls that's better to avoid. So, even if they are still supported, you should adopt the following approach.

The core key of the new coding guidelines is to retrieve an instance of the Concrete CMS service container, that is the instance of the Concrete\Core\Application\Application class.

If your code resides in a method of a class that uses the Concrete\Core\Application\ApplicationAwareTrait trait, it's as easy as using $this->app. In all the other cases, you can use this code:

$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();

In the remaining part of this documentation, we'll use $app for both the above cases.

Was this information useful?
Thank you for your feedback.