Hi,
When defining a virtual type in di.xml
, it seems that you can't use the associated Factory in dependency injection afterwards.
For example with the following definition (just here as an example, using factory with sessions is meaningless):
#File: app/code/Magento/Catalog/etc/di.xml
<virtualType name="Magento\Catalog\Model\Session\Storage" type="Magento\Framework\Session\Storage">
<arguments>
<argument name="namespace" xsi:type="string">catalog</argument>
</arguments>
</virtualType>
Attempting to inject a factory for that virtual type in a constructor
class MyClass {
public function __construct(
Magento\Catalog\Model\Session\StorageFactory $storageFactory
) {
//...
}
}
results in the following error message at runtime:
Class Magento\Catalog\Model\Session\StorageFactory does not exist
(I also stumbled upon another message in a similar situation:
Source class "\Magento\Catalog\Model\Session\Storage" for "Magento\Catalog\Model\Session\StorageFactory" generation does not exist.
)
If it's the desired behaviour, what's the solution to create new instances of a virtual type at runtime?
Thanks
@xi-ao thank you for your feedback.
The GitHub issue tracker is intended for technical issues only. Please refer to the Community Forums or the Magento Stack Exchange site for technical questions.
If you think this problem to be an issue, please format this issue according to the Issue reporting guidelines: with steps to reproduce, actual result and expected result.
Is the actual intent of Magento 2 push real community questions to the Community forum just so that they get forgotten about and never get further traction? There's not even a way for 'everyday' community members to comment on the Magento Community Forums. This has been asked in https://community.magento.com/t5/Magento-2-x-Programming/Use-a-generated-factory-with-virtual-types/td-p/50165 and seems to have been left there to die...
In case a solution is needed: you have to create a real factory class for your type.
It's not elegant (you lose the benefit of creating new types by configuration only), it's time consuming when you have a lot of virtual types to deal with, but at least it works.
But again, meh.
Most helpful comment
Is the actual intent of Magento 2 push real community questions to the Community forum just so that they get forgotten about and never get further traction? There's not even a way for 'everyday' community members to comment on the Magento Community Forums. This has been asked in https://community.magento.com/t5/Magento-2-x-Programming/Use-a-generated-factory-with-virtual-types/td-p/50165 and seems to have been left there to die...