use Silex\Application; use Doctrine\ORM\EntityManager; class MyApplication extends Application { public function __construct(array $values = []) { parent::__construct($values); $this['em'] = function ($c) { $config = new \Doctrine\ORM\Configuration(); $config->setMetadataDriverImpl( new \Doctrine\ORM\Mapping\Driver\AnnotationDriver( new \Doctrine\Common\Annotations\AnnotationReader() ) ); $config->setProxyDir('/tmp/proxies'); $config->setProxyNamespace('MyProject\Proxies'); $connectionOptions = array( 'dbname' => 'my_database', 'user' => 'my_username', 'password' => 'my_password', 'host' => 'localhost', 'driver' => 'pdo_mysql', ); $entityManager = EntityManager::create($connectionOptions, $config); return $entityManager; }; } } $app = new MyApplication([ 'debug' => true, ]); $app->get('/', function() use($app) { $students = $app['em']->getRepository(Student::class)->findAll(); return new JsonResponse(['students' => $students]); }); $app->run();
use Silex\Application; use Silex\ServiceProviderInterface; use Monolog\Logger; use Monolog\Handler\StreamHandler; class MonologServiceProvider implements ServiceProviderInterface { public function register(Application $app) { $app['monolog'] = function ($app) { $log = new Logger('my_app'); $log->pushHandler(new StreamHandler('/path/to/logs/my_app.log', Logger::DEBUG)); return $log; }; } public function boot(Application $app) { } } $app = new Application([ 'debug' => true, ]); $app->register(new MonologServiceProvider()); $app->get('/', function() use($app) { $app['monolog']->debug('Hello world'); return new Response('Hello world'); }); $app->run();This example shows how to create a custom Service Provider to use the Monolog logging library. The `MonologServiceProvider` class implements the `ServiceProviderInterface` and registers the `monolog` service with a new `Logger` object. The `$app` object can access the `Logger` object through the `monolog` service, as shown in the route callback function. In summary, the Application extend in Silex allows developers to extend the base Application class with their own functionality. This is particularly useful when integrating third-party packages or libraries with your Silex application. In both examples above, the third-party packages used were Doctrine ORM and Monolog logging library, respectively.