fabpot/Silex — pull request #790 by abeldebeer (2013-09-16 09:31:52+00:00)
Title: Adds support for class name Service Controller

With this addition, a service controller definition can be a class name:

$app['controller.example'] = 'Foo\Bar\ExampleController';
$app->get('/', 'controller.example:myMethod');

This addition is just some syntactic sugar, making the service controller definition simpler. It will especially shine with PHP 5.5's class name resolution (https://wiki.php.net/rfc/class_name_scalars):

$app['controller.example'] = ExampleController::class;

The ServiceControllerResolver creates a new instance of the class every time it is called, which usually would be 1 x per request. It assumes the controller class constructor accepts an app instance:

namespace Foo\Bar;

class ExampleController {
function __construct(\Silex\Application $app) {}
}

If you need the instance to be stored, and / or want to pass different arguments to the class's constructor, then use the default method:

$app['controller.example'] = $app->share(function() use($app) {
return new \Foo\Bar\ExampleController($app['service.one'], $app['service.two']);
});

This behavior is outlined here: http://silex.sensiolabs.org/doc/providers/service_controller.html#usage