Below you will find pages that utilize the taxonomy term “Buzz”
Silex: creating a service provider for Buzz
The class Silex\Application extends \Pimple, a very lean dependency injection container, which itself implements \ArrayAccess. This allows you to define parameters and services and retrieve them from the Application like keys in an array. The first time I defined a service in a Silex application, in my case the Buzz Browser, I did it “inline”, i.e. inside my app.php file. This is how I did it:
use Buzz\Browser;
$app = new Silex\Application();
$app['autoloader']->registerNamespace('Buzz', __DIR__.'/vendor/buzz/lib');
$app['buzz.client_class'] = 'Buzz\\Client\\Curl';
$app['browser'] = $app->share(function() use ($app) {
$clientClass = $app['buzz.client_class'];
return new Browser(new $clientClass);
});
// $app['browser'] is ready for use
But this is not very reusable; every Silex application that needs Buzz\Browser as a service, needs to take care of the autoloading, configure and define the service. The Browser service is not such a very complex service, but think about everything that needs to be done to define and configure a service like the Doctrine DBAL (of course, Silex has a DoctrineServiceProvider for that already…).