fortrabbit/slimcontroller — pull request #32 by acelaya (2014-11-24 16:48:44+00:00)
Title: Load controllers as services to allow dependency injection

I've made a small update to allow controllers to be loaded from Slim's DI container instead of being lazy instantiated every time.
This way controllers can be defined as shared services and dependency injection is possible.
What I've made is simply checking if the controller exists in the container and fetch it from there. Otherwise it will be instantiated.

A couple examples:

```php
$app->theController = function () use ($app) {
return MyController($app, $app->aService, $app->anotherService);
}
$app->addControllerRoute('/', 'theController:index');
```

This will fetch the `theController` service from the container and call its `index` action while dispatching the route.

```php
$app->container->singleton('ControllerThatDoesntExist', function () use ($app) {
return MyController($app, $app->aService, $app->anotherService);
});
$app->addRoute([
'/' => ['get' => 'ClassThatDoesntExist:hello']
]);
```

Similar to previous example, this will fetch the shared service `ClassThatDoesntExist`.

```php
$app->addRoute([
'/' => 'ClassThatExists:goodbye'
]);
```

In this case the controller exists and everything woirks as in previous versions.