What is the use of pagefactory in Magento 2?

Pagefactory is used to initialize the layout.

Its working in magento2 same like as in magento1 was doing with below code

$this->_view->loadLayout();

 $this->_view->renderLayout();

Unlike Magento 1.x in magento 2 introduces factories which are used to set and get data of given object. best practice is, instead of using object manager use factories.

Like that Magento perform all operations systematically without changing the flow of request. so if any module overriding the functionality should have latest data.


Page factory is used to create a page result by initializing the layout. Page factory create the response object in execute()

public function execute()
{
    return $this->pageResultFactory->create();
}

When you return a page result object from execute method, you’re telling Magento to kick off the standard layout handle XML file page rendering.