Does concrete5 support a dependency injection container

Permalink
Creating a package with multiple single pages. Would like to registrar services in the package controller and then invoke them in the single page controllers. Does concrete5 have something like pimple in place? Thanks

 
mnakalay replied on at Permalink Reply
mnakalay
C5 uses Laravel under the hood so you can do dependency injection and pretty much anything you can do with Laravel.

These 2 chapters from the docs might help:
https://documentation.concrete5.org/developers/extending-concrete5-w...
https://documentation.concrete5.org/developers/packages/overview...

As for dependency injection itself, read this one:https://documentation.concrete5.org/developers/dependency-injection/...
NotionCommotion replied on at Permalink Reply
Thanks mnakalay,

I looked briefly into Laravel a couple of years ago, but didn't go further. I think it is good that C5 uses it under the hood instead of trying to duplicate everything.

I think this is what I was looking for inspired by your last link.

Package controller:

$this->app->bind('someDescription', function(Application $app) use($config) {
    return new SomeClass($config['someSetting']);
});


And then in some page controller:

$someObject = $this->app->make('someDescription');


Look reasonable?
mnakalay replied on at Permalink Reply
mnakalay
Yes, provided 'SomeDescription' is actually an existing class, not an actual description :)

What that code does is, every time the class SomeDescription is instantiated using Laravel's container, the class SomeClass is returned instead with a specific config value.

Keep in mind that this will not work at all if someone instantiates SomeDescription by doing
new SomeDescription()

Instead of using Laravel's container $app->make().
NotionCommotion replied on at Permalink Reply
Perfect! I typically use a fully qualified classname instead of "SomeDescription", but with other frameworks only a unique string is required. And locate the definition scope in the package's controller I expect.
mnakalay replied on at Permalink Reply
mnakalay
Yes.
You can do
use Qualified\Class\Name;
$app->bind(Name::class, ...

OR
$app->bind(\Qualified\Class\Name::class, ...

OR
$app->bind('\Qualified\Class\Name', ...