{"id":18520131,"url":"https://github.com/php-di/silex-bridge","last_synced_at":"2025-07-17T02:07:19.663Z","repository":{"id":32590869,"uuid":"36174200","full_name":"PHP-DI/Silex-Bridge","owner":"PHP-DI","description":"PHP-DI integration in Silex","archived":false,"fork":false,"pushed_at":"2017-12-07T18:39:41.000Z","size":70,"stargazers_count":24,"open_issues_count":2,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-07-05T05:38:49.166Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://php-di.org/doc/frameworks/silex.html","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PHP-DI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-24T13:45:24.000Z","updated_at":"2024-05-15T11:31:27.000Z","dependencies_parsed_at":"2022-08-24T00:50:52.127Z","dependency_job_id":null,"html_url":"https://github.com/PHP-DI/Silex-Bridge","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/PHP-DI/Silex-Bridge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSilex-Bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSilex-Bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSilex-Bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSilex-Bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PHP-DI","download_url":"https://codeload.github.com/PHP-DI/Silex-Bridge/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSilex-Bridge/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265559951,"owners_count":23788103,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T17:18:41.262Z","updated_at":"2025-07-17T02:07:19.608Z","avatar_url":"https://github.com/PHP-DI.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP-DI integration with Silex\n\n[![Build Status](https://travis-ci.org/PHP-DI/Silex-Bridge.svg?branch=master)](https://travis-ci.org/PHP-DI/Silex-Bridge)\n[![Coverage Status](https://coveralls.io/repos/PHP-DI/Silex-Bridge/badge.svg)](https://coveralls.io/r/PHP-DI/Silex-Bridge)\n\n## Installation\n\n```\n$ composer require php-di/silex-bridge\n```\n\n## Usage\n\nIn order to benefit from PHP-DI's integration in Silex, you only need to use `DI\\Bridge\\Silex\\Application` instead of the original `Silex\\Application`.\n\nHere is the classic Silex example updated:\n\n```php\n\u003c?php\n\nrequire_once __DIR__.'/../vendor/autoload.php';\n\n$app = new DI\\Bridge\\Silex\\Application();\n\n$app-\u003eget('/hello/{name}', function ($name) use ($app) {\n    return 'Hello '.$app-\u003eescape($name);\n});\n\n$app-\u003erun();\n```\n\n## Benefits\n\nUsing PHP-DI in Silex allows you to use all the awesome features of PHP-DI to wire your dependencies (using the definition files, autowiring, annotations, …).\n\nAnother big benefit of the PHP-DI integration is the ability to use dependency injection inside controllers, middlewares and param converters:\n\n```php\nclass Mailer\n{\n    // ...\n}\n\n$app-\u003epost('/register/{name}', function ($name, Mailer $mailer) {\n    $mailer-\u003esendMail($name, 'Welcome!');\n\n    return 'You have received a new email';\n});\n\n// You can also inject the whole request object like in a traditional Silex application\n$app-\u003epost('/register/{name}', function (Request $request, Mailer $mailer) {\n    // ...\n});\n\n// Injection works for middleware too\n$app-\u003ebefore(function (Request $request, Mailer $mailer) {\n    // ...\n});\n\n// And param converters\n$app-\u003eget('/users/{user}', function (User $user) {\n    return new JsonResponse($user);\n})-\u003econvert('user', function ($user, UserManager $userManager) {\n    return $userManager-\u003efindById($user);\n});\n```\n\nDependency injection works using type-hinting:\n\n- it can be mixed with request parameters (`$name` in the example above)\n- the order of parameters doesn't matter, they are resolved by type-hint (for dependency injection) and by name (for request attributes)\n- it only works with objects that you can type-hint: you can't inject string/int values for example, and you can't inject container entries whose name is not a class/interface name (e.g. `twig` or `doctrine.entity_manager`)\n\n### Controllers as services\n\nWith Silex and Pimple, you can define [controllers as services](http://silex.sensiolabs.org/doc/providers/service_controller.html) by installing the `ServiceControllerServiceProvider` and using a specific notation.\n\nWith the PHP-DI bridge, you can natively define any type of callable based on services:\n\n- object method:\n\n```php\nclass HelloController\n{\n    public function helloAction($name)\n    {\n        // ...\n    }\n}\n\n$app-\u003eget('/{name}', [HelloController::class, 'helloAction']);\n```\n\nYou will notice above that we give the class name and not an object: PHP-DI will instantiate the instance (and inject dependencies inside it) only if it is used.\n\n- [invokable class](http://php.net/manual/en/language.types.callable.php)\n\n```php\nclass HelloController\n{\n    public function __invoke($name)\n    {\n        // ...\n    }\n}\n\n$app-\u003eget('/{name}', HelloController::class);\n```\n\nAgain you will notice that we pass the class name and not an instance. PHP-DI will correctly detect that this is an invokable class and will instantiate it.\n\n### Middlewares, route variable converters, error handlers and view handlers\n\nThe callable resolution described above (for \"controllers as services\") applies for registering other Silex objects:\n\n- [middlewares](http://silex.sensiolabs.org/doc/middlewares.html)\n- [route variable converters](http://silex.sensiolabs.org/doc/usage.html#route-variable-converters)\n- [error handlers](http://silex.sensiolabs.org/doc/usage.html#error-handlers)\n- [view handlers](http://silex.sensiolabs.org/doc/usage.html#view-handlers)\n\nFor example you can define a middleware like so and let PHP-DI instantiate it:\n\n```php\nclass AuthMiddleware\n{\n    public function beforeRoute(Request $request, Application $app)\n    {\n        // ...\n    }\n}\n\n$app-\u003ebefore([AuthMiddleware::class, 'beforeRoute']);\n```\n\n## Configuring the container\n\nYou can configure PHP-DI's container by creating your own `ContainerBuilder` and passing it to the application:\n\n```php\n$containerBuilder = new DI\\ContainerBuilder();\n\n// E.g. setup a cache\n$containerBuilder-\u003esetDefinitionCache(new ApcCache());\n\n// Add definitions\n$containerBuilder-\u003eaddDefinitions([\n    // place your definitions here\n]);\n\n// Register a definition file\n$containerBuilder-\u003eaddDefinitions('config.php');\n\n$app = new DI\\Bridge\\Silex\\Application($containerBuilder);\n```\n\n## Silex service providers\n\nSilex offers several \"service providers\" to pre-configure some 3rd party libraries, for example Twig or Doctrine. You can still use those service providers with this integration (even though in bigger projects you might want to configure everything yourself).\n\nHere is the example of the [TwigServiceProvider](http://silex.sensiolabs.org/doc/providers/twig.html):\n\n```php\n$app-\u003eregister(new Silex\\Provider\\TwigServiceProvider(), [\n    'twig.path' =\u003e __DIR__ . '/views',\n]);\n\n$app-\u003eget('/', function () use ($app) {\n    return $app['twig']-\u003erender('home.twig');\n});\n```\n\nSince Twig services are registered using a custom name instead of the actual class name (e.g. `twig` instead of the `Twig_Environment` class name), you cannot inject such dependencies into closures. If you want to inject in controller closures, you can alias entries with PHP-DI:\n\n```php\n$builder = new ContainerBuilder();\n\n$builder-\u003eaddDefinitions([\n    'Twig_Environment' =\u003e \\DI\\get('twig'), // alias\n]);\n\n// ...\n\n// Twig can now be injected in closures:\n$app-\u003epost('/', function (Twig_Environment $twig) {\n    return $twig-\u003erender('home.twig');\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-di%2Fsilex-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-di%2Fsilex-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-di%2Fsilex-bridge/lists"}