Reading list Switch to dark mode

    What is PageFactory in Magento 2?

    Updated 14 February 2023

    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.

    Start your headless eCommerce
    now.
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home