php-di/slim-bridge
Composer 安装命令:
composer require php-di/slim-bridge
包简介
PHP-DI integration in Slim
README 文档
README
This package configures Slim to work with the PHP-DI container.
The full documentation is here: http://php-di.org/doc/frameworks/slim.html
Why?
PHP-DI as a container
The most obvious difference with the default Slim installation is that you will be using PHP-DI as the container, which has the following benefits:
- autowiring
- powerful configuration format
- support for modular systems
- ...
If you want to learn more about all that PHP-DI can offer have a look at its introduction.
Controllers as services
While your controllers can be simple closures, you can also write them as classes and have PHP-DI instantiate them only when they are called:
class UserController { private $userRepository; public function __construct(UserRepository $userRepository) { $this->userRepository = $userRepository; } public function delete($request, $response) { $this->userRepository->remove($request->getAttribute('id')); $response->getBody()->write('User deleted'); return $response; } } $app->delete('/user/{id}', ['UserController', 'delete']);
Dependencies can then be injected in your controller using autowiring, PHP-DI config files or even annotations.
Controller parameters
By default, Slim controllers have a strict signature: $request, $response, $args. The PHP-DI bridge offers a more flexible and developer friendly alternative.
Controller parameters can be any of these things:
- the request or response (parameters must be named
$requestor$response) - route placeholders
- request attributes
- services (injected by type-hint)
You can mix all these types of parameters together too. They will be matched by priority in the order of the list above.
Request or response injection
You can inject the request or response in the controller parameters by name:
$app->get('/', function (ResponseInterface $response, ServerRequestInterface $request) { // ... });
As 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.
Route placeholder injection
$app->get('/hello/{name}', function ($name, ResponseInterface $response) { $response->getBody()->write('Hello ' . $name); return $response; });
As 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.
Request attribute injection
$app->add(function (ServerRequestInterface $request, RequestHandlerInterface $handler) { $request = $request->withAttribute('name', 'Bob'); $response = $handler->handle($request); return $response; }); $app->get('/', function ($name, ResponseInterface $response) { $response->getBody()->write('Hello ' . $name); return $response; });
As 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.
Service injection
To 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.
You can inject services by type-hinting them:
$app->get('/', function (ResponseInterface $response, Twig $twig) { return $twig->render($response, 'home.twig'); });
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->get(/* the type-hinted class */).
Documentation
The documentation can be read here: http://php-di.org/doc/frameworks/slim.html
php-di/slim-bridge 适用场景与选型建议
php-di/slim-bridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.22M 次下载、GitHub Stars 达 177, 最近一次更新时间为 2015 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 php-di/slim-bridge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-di/slim-bridge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.22M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 183
- 点击次数: 16
- 依赖项目数: 110
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-28