{"id":18520123,"url":"https://github.com/php-di/slim-bridge","last_synced_at":"2025-05-15T00:07:46.488Z","repository":{"id":46292503,"uuid":"48714109","full_name":"PHP-DI/Slim-Bridge","owner":"PHP-DI","description":"PHP-DI integration with the Slim framework","archived":false,"fork":false,"pushed_at":"2024-11-29T22:13:42.000Z","size":75,"stargazers_count":175,"open_issues_count":6,"forks_count":39,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-13T21:41:35.908Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://php-di.org/doc/frameworks/slim.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-28T21:51:32.000Z","updated_at":"2025-03-01T13:18:17.000Z","dependencies_parsed_at":"2024-06-18T11:00:46.267Z","dependency_job_id":"ccca9d96-fe29-4e1a-829b-a2ec14e61e84","html_url":"https://github.com/PHP-DI/Slim-Bridge","commit_stats":{"total_commits":81,"total_committers":16,"mean_commits":5.0625,"dds":"0.37037037037037035","last_synced_commit":"02ab0274a19d104d74561164f8915b62d93f3cf0"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSlim-Bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSlim-Bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSlim-Bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHP-DI%2FSlim-Bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PHP-DI","download_url":"https://codeload.github.com/PHP-DI/Slim-Bridge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254249198,"owners_count":22039029,"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:40.817Z","updated_at":"2025-05-15T00:07:46.446Z","avatar_url":"https://github.com/PHP-DI.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP-DI integration with Slim\n\nThis package configures Slim to work with the [PHP-DI container](http://php-di.org/).\n\n[![Build Status](https://travis-ci.org/PHP-DI/Slim-Bridge.svg?branch=master)](https://travis-ci.org/PHP-DI/Slim-Bridge)\n[![](https://img.shields.io/packagist/dt/php-di/slim-bridge.svg)](https://packagist.org/packages/php-di/slim-bridge)\n\nThe full documentation is here: **http://php-di.org/doc/frameworks/slim.html**\n\n## Why?\n\n### PHP-DI as a container\n\nThe most obvious difference with the default Slim installation is that you will be using PHP-DI as the container, which has the following benefits:\n\n- [autowiring](http://php-di.org/doc/autowiring.html)\n- powerful [configuration format](http://php-di.org/doc/php-definitions.html)\n- support for [modular systems](http://php-di.org/doc/definition-overriding.html)\n- ...\n\nIf you want to learn more about all that PHP-DI can offer [have a look at its introduction](http://php-di.org/).\n\n### Controllers as services\n\nWhile your controllers can be simple closures, you can also **write them as classes and have PHP-DI instantiate them only when they are called**:\n\n```php\nclass UserController\n{\n    private $userRepository;\n    \n    public function __construct(UserRepository $userRepository)\n    {\n        $this-\u003euserRepository = $userRepository;\n    }\n\n    public function delete($request, $response)\n    {\n        $this-\u003euserRepository-\u003eremove($request-\u003egetAttribute('id'));\n        \n        $response-\u003egetBody()-\u003ewrite('User deleted');\n        return $response;\n    }\n}\n\n$app-\u003edelete('/user/{id}', ['UserController', 'delete']);\n```\n\nDependencies can then be injected in your controller using [autowiring, PHP-DI config files or even annotations](http://php-di.org/doc/definition.html).\n\n### Controller parameters\n\nBy default, Slim controllers have a strict signature: `$request, $response, $args`. The PHP-DI bridge offers a more flexible and developer friendly alternative.\n\nController parameters can be any of these things:\n\n- the request or response (parameters must be named `$request` or `$response`)\n- route placeholders\n- request attributes\n- services (injected by type-hint)\n\nYou can mix all these types of parameters together too. They will be matched by priority in the order of the list above.\n\n#### Request or response injection\n\nYou can inject the request or response in the controller parameters by name:\n\n```php\n$app-\u003eget('/', function (ResponseInterface $response, ServerRequestInterface $request) {\n    // ...\n});\n```\n\nAs you can see, the order of the parameters doesn't matter. That allows to skip injecting the `$request` if it's not needed for example.\n\n#### Route placeholder injection\n\n```php\n$app-\u003eget('/hello/{name}', function ($name, ResponseInterface $response) {\n    $response-\u003egetBody()-\u003ewrite('Hello ' . $name);\n    return $response;\n});\n```\n\nAs you can see above, the route's URL contains a `name` placeholder. By simply adding a parameter **with the same name** to the controller, PHP-DI will directly inject it.\n\n#### Request attribute injection\n\n```php\n$app-\u003eadd(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {\n    $request = $request-\u003ewithAttribute('name', 'Bob');\n    $response = $handler-\u003ehandle($request);\n    return $response;\n});\n\n$app-\u003eget('/', function ($name, ResponseInterface $response) {\n    $response-\u003egetBody()-\u003ewrite('Hello ' . $name);\n    return $response;\n});\n```\n\nAs you can see above, a middleware sets a `name` attribute. By simply adding a parameter **with the same name** to the controller, PHP-DI will directly inject it.\n\n#### Service injection\n\nTo inject services into your controllers, you can write them as classes. But if you want to write a micro-application using closures, you don't have to give up dependency injection either.\n\nYou can inject services by type-hinting them:\n\n```php\n$app-\u003eget('/', function (ResponseInterface $response, Twig $twig) {\n    return $twig-\u003erender($response, 'home.twig');\n});\n```\n\n\u003e Note: you can only inject services that you can type-hint and that PHP-DI can provide. Type-hint injection is simple, it simply injects the result of `$container-\u003eget(/* the type-hinted class */)`.\n\n## Documentation\n\nThe documentation can be read here: **http://php-di.org/doc/frameworks/slim.html**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-di%2Fslim-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-di%2Fslim-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-di%2Fslim-bridge/lists"}