jenssegers/lean
Composer 安装命令:
composer require jenssegers/lean
包简介
Use the league/container with auto-wiring support as the core container in Slim 3
关键字:
README 文档
README
Lean allows you to use the PHP League's Container package with auto-wiring support as the core container in Slim 3.
Install
Via Composer
$ composer require jenssegers/lean
Usage
The easiest way to start using Lean is simply creating a Jenssegers\Lean\App instance:
require 'vendor/autoload.php'; $app = new \Jenssegers\Lean\App(); $app->get('/hello/{name}', function (Request $request, Response $response, string $name) { return $response->write('Hello, ' . $name); }); $app->run();
Behind the scenes, a Slim application is bootstrapped by adding all of the required Slim components to League's container.
Service Providers
Service 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.
To build a service provider it is as simple as extending the base service provider and defining what you would like to register.
use League\Container\ServiceProvider\AbstractServiceProvider; class SomeServiceProvider extends AbstractServiceProvider { /** * The provided array is a way to let the container * know that a service is provided by this service * provider. Every service that is registered via * this service provider must have an alias added * to this array or it will be ignored. */ protected $provides = [ SomeInterface::class, ]; /** * This is where the magic happens, within the method you can * access the container and register or retrieve anything * that you need to, but remember, every alias registered * within this method must be declared in the `$provides` array. */ public function register() { $this->getContainer() ->add(SomeInterface::class, SomeImplementation::class); } }
To 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.
$app = new \Jenssegers\Lean\App(); $app->getContainer()->addServiceProvider(\Acme\ServiceProvider\SomeServiceProvider::class);
Read more about service providers here.
Settings
You can access Slim's internal configuration through the settings key on the container:
$app = new \Jenssegers\Lean\App(); $app->getContainer()->get('settings')['displayErrorDetails'] = true;
Alternatively, an alias is registered that allows a bit more fluent way of working with settings:
$app = new \Jenssegers\Lean\App(); $app->getContainer()->get(\Slim\Settings::class)->set('displayErrorDetails', true);
Read more about the available configuration options here.
Route arguments
By 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).
Route arguments will be passed as individual arguments to your method:
$app->get('/books/{id}', function (Request $request, Response $response, string $id) { ... });
They are also accessible through the getAttribute method.
$app->get('/books/{id}', function (Request $request, Response $response) { $id = $request->getAttribute('id'); .... });
If 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:
$app->getContainer()->get(\Slim\Settings::class)->set('methodInjection', false);
Read more about routes here.
Error Handlers
By default, Lean uses Slim's error handlers. There are different ways to implement an error handler for Slim, read more about them here.
Typically you would create a custom error handler class that looks like this:
class CustomErrorHandler { public function __invoke(ServerRequestInterface $request, Response $response, Throwable $exception) { return $response->withJson([ 'error' => 'Something went wrong', ], 500); } }
Then you overwrite the default handler by adding it to the container:
$app = new Jenssegers\Lean\App(); $app->getContainer()->share('errorHandler', function () { return new CustomErrorHandler(); });
Ideally, you would put this code inside a service provider. Read more about service providers above.
Testing
$ php ./vendor/bin/phpunit
License
The MIT License (MIT).
jenssegers/lean 适用场景与选型建议
jenssegers/lean 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.43k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2015 年 11 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「slim」 「league」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jenssegers/lean 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jenssegers/lean 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jenssegers/lean 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Secure session middleware for Slim 3 framework
An attribute based container for league/container
Handle the output of complex data structures ready for API output.
A fast and intuitive dependency injection container.
Vkontakte provider for league/oauth2-client
Adds request-parameter validation to the SLIM 3.x PHP framework
统计信息
- 总下载量: 3.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-22