2 Comments

Magento 2: Clear Direction on Controller Result Factories

Magento 2 CLI is broken
custom advanced search

In a controller’s execute method, has there been any clear guidance from the Magento core team if client developers should

  1. Be using the individual PageFactory, JsonFactory, RawFactory, etc objects

  2. Or using the more generic MagentoFrameworkControllerResultFactory object along with the ResultFactory::_TYPE constants

when they want to instantiate an object to return?

If you’re not familiar with the issues/confusion around this, I’m talking about the things in the following post. It’s not clear if, going forward, Magento’s supporting all these methods, or if there’s a particular style/technique they prefer (and will support) going forward.

Related Posts

  • Magento 2 Load – Block (CMS Static Block in Magento 1 terms) (1)
    I am attempting to load a static block from my controller - but am having trouble locating the right syntax for doing this. What is the proper syntax to load a CMS block by identifier or block id and what factory supports this type of loading?
  • Difference between Factory vs ObjectManager while instance object (1)
    I have some confusing while use Factory pattern vs new instance object. What difference between them? $object = objectManager->create() vs objectManager->create('ObjectFactory') what benefits in cases when create object by Factory. This thread may be large but I need core […]
  • Factory methods, should I manually create them? (1)
    So when I develop extensions I usually do this outside of Magento and just pull in the packages I need. Once I have developed the extension I integrate it at the end. Because of this I manually create factory methods, is this best practice or not? I don't really want to couple to […]
  • Factory methods, should I manually create them? (1)
    So when I develop extensions I usually do this outside of Magento and just pull in the packages I need. Once I have developed the extension I integrate it at the end. Because of this I manually create factory methods, is this best practice or not? I don't really want to couple to […]
  • Factory methods, should I manually create them? (1)
    So when I develop extensions I usually do this outside of Magento and just pull in the packages I need. Once I have developed the extension I integrate it at the end. Because of this I manually create factory methods, is this best practice or not? I don't really want to couple to […]
  • Factory methods, should I manually create them? (1)
    So when I develop extensions I usually do this outside of Magento and just pull in the packages I need. Once I have developed the extension I integrate it at the end. Because of this I manually create factory methods, is this best practice or not? I don't really want to couple to […]
  • Magento 2 – Customizing Pdf Invoice (0)
    How would I go about adding a custom message to the generated pdf invoice? I'm seeing a number of available extensions for Magento 2 but hardly any documentation about how I could achieve this myself. I've made a start by extending the Sales module, MagentoSalesModelOrderPdfInvoice […]
  • setup:upgrade can't find Factory (1)
    my Magento 2 runs in developer mode and I'm trying to install my first selfwritten extension by CLI. The install itself works nice, but when running the setup:upgrade command, it can't find the injected Factories in my UpgradeData.php scripts. Here a condensed version of the […]
  • Magento 2: When will 2.0 support stop? (4)
    Right now the Magento team supports 6 different versions of Magento: 1.9.3.0 CE 1.14.3.0 EE 2.0.10 CE 2.0.10 EE 2.1.2 CE 2.1.2 EE I reckon it's a lot of work to deal with and 2.2 is coming next. So my questions are: will Magento 2.0 support stop when 2.2 is released ? if not, […]
  • Magento 2: When will 2.0 support stop? (4)
    Right now the Magento team supports 6 different versions of Magento: 1.9.3.0 CE 1.14.3.0 EE 2.0.10 CE 2.0.10 EE 2.1.2 CE 2.1.2 EE I reckon it's a lot of work to deal with and 2.2 is coming next. So my questions are: will Magento 2.0 support stop when 2.2 is released ? if not, […]
Magento 2 CLI is broken
custom advanced search

2 Comments on this article

  • User November 12, 2016

    Both methods will be supported in foreseeable future.

    Generic factory was introduced first. It’s easy to work with it especially when your action controller might return different result types depending on request data.

    Specific factory types were introduced to solve problems with type safety and auto-complete. It’s less convenient to work with specific factories (1 constructor dependency per return type), so generic factory was kept as an alternative.

    Idea to add specific factory methods to generic factory for each result type is being discussed now. It will solve both problems: convenience and type-safety.

    Reply

  • User November 12, 2016

    I always favor the specific individual factories, because of the following benefits:

    • They give proper method auto-completion on the created result object without a PHPDoc type hint (once the factory has been generated). A PHPDoc type hint can be used to work around that, but many comments become lies over time when the implementation changes, but the comments are not updated.
    • The result types that the action controller might return are clear to see by looking at the constructor parameters.
    • Less coupling to implementation details of the generic ResultFactory (e.g. the type code constants).
    • Less coupling with the parent Action class by relying on protected properties, so one less potential breaking point during upgrades.
    • Easier to write readable tests (no willReturnMap() for a mockResultFactory is required).

    Reply

Add a comment