Dependency Injection Overview

Yee has a built-in resource locator, providing an easy way to inject objects into a Yee app, or to override any of the Yee application's internal objects (e.g. Request, Response, Log).

Injecting simple values

If you want to use Yee as a simple key-value store, it is as simple as this:

$app = new \Yee\Yee();
$app->foo = 'bar';

Now, you can fetch this value anywhere with $app->foo and get its value bar.

Using the resource locator

You can also use Yee as a resource locator by injecting closures that define how your desired objects will be constructed. When the injected closure is requested, it will be invoked and the closure's return value will be returned.

$app = new \Yee\Yee();

// Determine method to create UUIDs
$app->uuid = function () {
    return exec('uuidgen');

// Get a new UUID
$uuid = $app->uuid;

Singleton resources

Sometimes, you may want your resource definitions to stay the same each time they are requested (i.e. they should be singletons within the scope of the Yee app). This is easy to do:

$app = new \Yee\Yee();

// Define log resource
$app->container->singleton('log', function () {
    return new \My\Custom\Log();

// Get log resource
$log = $app->log;

Every time you request the log resource with $app->log, it will return the same instance.

Closure resources

What if you want to literally store a closure as the raw value and not have it invoked? You can do that like this:

$app = new \Yee\Yee();

// Define closure
$app->myClosure = $app->container->protect(function () {});

// Return raw closure without invoking it
$myClosure = $app->myClosure;