问题标签 [php-di]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
php - 使用 PHP-DI 将变量自动注入到类方法中
我一直在使用 PHP-DI,我想询问是否有一种方法可以自动将变量注入各种类方法,即使用自动装配。
谢谢
php - 在 PHP-DI 中,has() 函数不适用于文件中的定义
使用 php 定义文件,我创建了这个定义
但是当我使用 has() 函数来检查定义是否存在时,即
但 get() 函数设法返回对象。
编辑:应用程序有点复杂,所以不能把所有代码都放在这里,但它意味着绕过我在以这种方式实现定义时遇到的错误
错误是:
致命错误:未捕获的异常 'InvalidArgumentException' 带有消息 'ContainerBuilder::addDefinitions() 参数必须是字符串或实现 ChainableDefinitionSource,给定数组
感谢您及时的回复。
php - 如何将 Twig 对象从容器中取出?
我正在使用带有 PHP-DI/Slim-Bridge 的 Slim Framework 3。这是我的容器:
如何将树枝对象从容器中取出?我尝试了 $container->twig,但无法取回对象。
php - 使用 PHP-DI 将定义的参数注入构造函数时遇到问题
这可能看起来很简陋,但我似乎无法在不使用注释的情况下直接将任何参数注入到我的类构造函数中。下面是定义和类调用
但我不断收到此错误
无法解析带有消息“条目“命名空间\到产品”的未捕获异常“异常”:__construct() 的参数 $root 没有定义或猜测的值
但是,如果我使用注释,这个问题将得到解决。但我真的不想在每次注入参数时都使用注释。
这里有什么问题?
谢谢
php - PHP-DI 找不到“接口名称”的条目或类
所以,我第一次尝试设置 php-di,但我在构建器方面遇到了一些问题。我不断收到错误:
我的容器设置哪里出错了?
pimcore - Pimcore 和依赖注入
我想在 Pimcore 中使用依赖注入。幸运的是,自 Pimcore 4.x 以来,这是开箱即用的。
但我必须承认我无法让它发挥作用。我读了这些网站:DI,DI,DI
以及 php-di 的文档,因为 Pimcore 正在使用它。
现在我有一个插件,一个简单的搜索,我想注入到我的一个控制器中。
插件结构如下所示:
我想将里面的类注入Search.php
到我的控制器中。
类本身没有命名空间,但在 plugin.xml 中定义了一个
我复制了di.example.php
它website/config/di.php
,它被称为,我对此非常满意。
我的定义如下所示:
我的控制器:
我什至尝试直接在pimcore/lib/Pimcore.php
in 中设置定义function getDiContainer()
但没有成功。
有任何想法吗?谢谢你,祝你有美好的一天!
php - Adding one regular variable to auto-wired function
I'm using Slim Framework with PHP-DI to autowire dependencies for me. But one dependency is just a regular array. If I put a regular array into my container configuration, then all arrays will be set to that one array. So my primary question would be:
How do I inject just one single variable, while letting the container auto-wire the rest? Is this possible? I've found myself writing a route like this:
Whereas all my other routes are short like this, because they only have dependencies on unique classes:
I wrote all that extra stuff just to squeeze $myArray
into the configView function, is there a way to combine regular dependency injection with autowiring? Does any framework or library do that?
I could have just written it like this if I didn't need that one array:
Alternatively, I could reach into the container and get the array, but that would make the page-function dependent on the container, which is something which should be avoided.
php - PHP-DI injectOn 不注入 setter 方法
我在使用 配置容器时设置了依赖项和定义ContainerBuilder
,然后编译它以获得实际的Container
.
我是否错过了injectOn()
方法的概念,或者我在这里做错了什么($this->translator
仍未分配)?我尝试了不同的方法,既实例化类并将对象添加到,ContainerBuilder
并将其作为\DI\object()
定义传递,两者都具有相同的结果。
php - Partial dependency injection
In a MVC context, I have a controller that depends on a service, the service in turn depends on a data_source (in the specific case, a client to fetch data from a third-party API).
In order to instantiate the service with a mock data_source when testing, the service's constructor expects a data_source. The same holds for the controller, whose constructor expects a service.
When creating a controller, I want to pass it a request object as well, because I'd prefer this
new Controller(request, service).action_name
to this
new Controller(service).action_name(request)
Achieving this without using any container for Dependency Injection is trivial.
What I don't understand is how to do so using php-di
My objective is to have the service injected into the controller by the container, while passing the request object to the controller myself.
UPDATE 1
This is my ApplicationController
#xA;Foo follows
#xA;This my Request
#xA;And this is my attempt to get DI to work as I'd like:
#xA;This is the error I get:
#xA;N.B.: the error stays the same if I type-hint the request and/or switch the order of params in the constructor
Looking at the error, I infer that the ::call() solution proposed by Matthew Napoli works if I instantiate the controller with just the service and pass the request as a parameter for the action method.
Does this mean that I can't rely on the container for "partial" injection?
UPDATE 2
For the solution described in this update, please look at my own answer to the question
php - PHP-DI - 工厂和对象之间的差异
PHP-DI 允许一些方法来定义注入,包括工厂和对象: http: //php-di.org/doc/php-definitions.html。
工厂:
实例是惰性创建的TestClass
,仅在需要时才创建。
对象:
如果使用Objects,实例也是懒惰创建的吗?
如果是,工厂和对象有什么区别?