{"id":21829847,"url":"https://github.com/jenssegers/lean","last_synced_at":"2025-04-14T06:20:36.793Z","repository":{"id":56997995,"uuid":"46636160","full_name":"jenssegers/lean","owner":"jenssegers","description":"Use the PHP League's Container package with auto-wiring support as the core container in Slim 3","archived":false,"fork":false,"pushed_at":"2019-01-14T18:06:18.000Z","size":45,"stargazers_count":31,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-27T20:06:02.020Z","etag":null,"topics":["container-interop","dependency-injection","league","php-league","slim"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jenssegers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-21T22:15:18.000Z","updated_at":"2024-02-04T06:05:33.000Z","dependencies_parsed_at":"2022-08-21T11:10:16.260Z","dependency_job_id":null,"html_url":"https://github.com/jenssegers/lean","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenssegers%2Flean","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenssegers%2Flean/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenssegers%2Flean/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenssegers%2Flean/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jenssegers","download_url":"https://codeload.github.com/jenssegers/lean/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248830783,"owners_count":21168350,"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":["container-interop","dependency-injection","league","php-league","slim"],"created_at":"2024-11-27T18:32:40.674Z","updated_at":"2025-04-14T06:20:36.746Z","avatar_url":"https://github.com/jenssegers.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Lean\n====\n\n[![Latest Stable Version](http://img.shields.io/packagist/v/jenssegers/lean.svg)](https://packagist.org/packages/jenssegers/lean) [![Build Status](http://img.shields.io/travis/jenssegers/lean.svg)](https://travis-ci.org/jenssegers/lean) [![Coverage Status](http://img.shields.io/coveralls/jenssegers/lean.svg)](https://coveralls.io/r/jenssegers/lean)\n\nLean allows you to use the [PHP League's Container](https://github.com/thephpleague/container) package with auto-wiring support as the core container in [Slim 3](https://github.com/slimphp/Slim).\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require jenssegers/lean\n```\n\n## Usage\n\nThe easiest way to start using Lean is simply creating a `Jenssegers\\Lean\\App` instance:\n\n``` php\nrequire 'vendor/autoload.php';\n\n$app = new \\Jenssegers\\Lean\\App();\n\n$app-\u003eget('/hello/{name}', function (Request $request, Response $response, string $name) {\n    return $response-\u003ewrite('Hello, ' . $name);\n});\n\n$app-\u003erun();\n```\n\nBehind the scenes, a Slim application is bootstrapped by adding all of the required Slim components to League's container.\n\n## Service Providers\n\nService providers give the benefit of organising your container definitions along with an increase in performance for larger applications as definitions registered within a service provider are lazily registered at the point where a service is retrieved.\n\nTo build a service provider it is as simple as extending the base service provider and defining what you would like to register.\n\n```php\nuse League\\Container\\ServiceProvider\\AbstractServiceProvider;\n\nclass SomeServiceProvider extends AbstractServiceProvider\n{\n    /**\n     * The provided array is a way to let the container\n     * know that a service is provided by this service\n     * provider. Every service that is registered via\n     * this service provider must have an alias added\n     * to this array or it will be ignored.\n     */\n    protected $provides = [\n        SomeInterface::class,\n    ];\n\n    /**\n     * This is where the magic happens, within the method you can\n     * access the container and register or retrieve anything\n     * that you need to, but remember, every alias registered\n     * within this method must be declared in the `$provides` array.\n     */\n    public function register()\n    {\n        $this-\u003egetContainer()\n            -\u003eadd(SomeInterface::class, SomeImplementation::class);\n    }\n}\n```\n\nTo register this service provider with the container simply pass an instance of your provider or a fully qualified class name to the League\\Container\\Container::addServiceProvider method.\n\n```php\n$app = new \\Jenssegers\\Lean\\App();\n$app-\u003egetContainer()-\u003eaddServiceProvider(\\Acme\\ServiceProvider\\SomeServiceProvider::class);\n```\n\nRead more about service providers [here](https://container.thephpleague.com/3.x/service-providers/).\n\n## Settings\n\nYou can access Slim's internal configuration through the `settings` key on the container:\n\n```php\n$app = new \\Jenssegers\\Lean\\App();\n\n$app-\u003egetContainer()-\u003eget('settings')['displayErrorDetails'] = true;\n```\n\nAlternatively, an alias is registered that allows a bit more fluent way of working with settings:\n\n```php\n$app = new \\Jenssegers\\Lean\\App();\n\n$app-\u003egetContainer()-\u003eget(\\Slim\\Settings::class)-\u003eset('displayErrorDetails', true);\n``` \n\nRead more about the available configuration options [here](https://www.slimframework.com/docs/v3/objects/application.html#slim-default-settings).\n\n# Route arguments\n\nBy default, Lean will use method injection to pass arguments to your routes. This allows you to type-hint dependencies on method level (similar to the Laravel framework).\n\nRoute arguments will be passed as individual arguments to your method:\n\n```php\n$app-\u003eget('/books/{id}', function (Request $request, Response $response, string $id) {\n    ...\n});\n```\n\nThey are also accessible through the `getAttribute` method.\n\n```php\n$app-\u003eget('/books/{id}', function (Request $request, Response $response) {\n    $id = $request-\u003egetAttribute('id');\n    ....\n});\n```\n\nIf you want to disable this behaviour and use the default Slim way of route arguments, you can disable this feature be setting `methodInjection` to `false`:\n\n```php\n$app-\u003egetContainer()-\u003eget(\\Slim\\Settings::class)-\u003eset('methodInjection', false);\n```\n\nRead more about routes [here](http://www.slimframework.com/docs/v3/objects/router.html).\n\n## Error Handlers\n\nBy default, Lean uses Slim's error handlers. There are different ways to implement an error handler for Slim, read more about them [here](https://www.slimframework.com/docs/v3/handlers/error.html).\n\nTypically you would create a custom error handler class that looks like this:\n\n```php\nclass CustomErrorHandler\n{\n    public function __invoke(ServerRequestInterface $request, Response $response, Throwable $exception)\n    {\n        return $response-\u003ewithJson([\n            'error' =\u003e 'Something went wrong',\n        ], 500);\n    }\n}\n```\n\nThen you overwrite the default handler by adding it to the container:\n\n```php\n$app = new Jenssegers\\Lean\\App();\n\n$app-\u003egetContainer()-\u003eshare('errorHandler', function () {\n    return new CustomErrorHandler();\n});\n```\n\nIdeally, you would put this code inside a service provider. Read more about service providers above.\n\n## Testing\n\n``` bash\n$ php ./vendor/bin/phpunit\n```\n\n## License\n\nThe MIT License (MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenssegers%2Flean","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjenssegers%2Flean","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenssegers%2Flean/lists"}