allenlinatoc/php-container
Composer 安装命令:
composer require allenlinatoc/php-container
包简介
PSR-11 compliant container class specifically built for Slim v4, on top of Pimple.
README 文档
README
PSR-11 compliant container class specifically built for Slim v4, on top of Pimple.
Installation
Warning: This is not yet Production-ready.
composer require allenlinatoc/php-container "v1.0.1-beta"
Usage
AppFactory::setContainer(new Allenlinatoc\PhpContainer\Container());
Now use it like how you are used to Pimple container.
Assuming you have a Twig PHP file as a service called twig.php:
<?php
return function ($container) {
return new \Slim\Views\Twig(
\Highdesk\Utils\FileSystem::root("/public/views"),
[
"cache" => \Highdesk\Utils\FileSystem::root("/.twigcache")
]
);
};
You can load it inside container like these:
// As array
$container["view"] = (require "twig.php");
// Through "set" method (like PHP-DI's way)
$container->set("view", (require "twig.php"));
// Or as direct property assignment (this is where __set() magic method kicks in)
$container->view = (require "twig.php");
Now inside Slim's route, you can pull your Twig service like these...
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
// As Array
$this["view"]->render($response, 'index.phtml', compact('name'));
// Through "get" method (like PHP-DI's way)
$this->get("view")->render($response, 'index.phtml', compact('name'));
// Or as property (via __get() magic method)
$this->view->render($response, 'index.phtml', compact('name'));
return $response;
});
Explanation
So I'm relatively new to Slim 4 framework. When I was dealing with dependency injection, using PHP-DI container wasn't good enough for me like how I was used to Pimple container.
In short, I was limited with PSR-11 kind of container e.g. I couldn't do direct property call for any container element:
$this->container->set("views", (require_once twig.php));
Now when I try to call Twig service as property...
$this->view->render(...)
It would tell me undefined property view - so I ended up with this simple Container class.
It extends from \Pimple\Container and implements ContainerInterface so that it would be usable with Slim 4's new
way of loading accommodating container: AppFactory::setContainer($container)
allenlinatoc/php-container 适用场景与选型建议
allenlinatoc/php-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.58k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 allenlinatoc/php-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 allenlinatoc/php-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-10