Page factory is used to create a page result by initializing the layout. Page factory creates the response object in execute() method for the controller.
Specific resultPageFactory is an instance of \Magento\Framework\View\Result\PageFactory
and when calling create on that class it returns an instance \Magento\Framework\View\Result\Page
described above. When this is used and the result is returned it means that your action will return HTML. It has somehow a similar similar effect as $this->loadLayout(); from Magento 1. When you call create on the resultPageFactory
object it actually loads the layout.
public function execute() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $this->pageResultFactory = $objectManager->create(\Magento\Framework\View\Result\PageFactory); return $this->pageResultFactory->create(); }
What is the factory in Magento 2?
As you know in OOP, a factory method will be used to instantiate an object. In Magento 2, the Magento 2 Factory Object does the same thing. To instantiate a model object we will use automatic constructor dependency injection to inject a factory object, then use a factory object to instantiate the model object.