Question

Although I refer some blog of pagefactory, I don't understand the use of page factory.

There is one code in the controller. Could you please explain to me how it works?

public function execute()
{
    return $this->_pageFactory->create();
}
Was it helpful?

Solution

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();

OTHER TIPS

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.

Magento2 uses the Factory Method Design Pattern which states that define and an interface for creating an object, but let subclasses decide which class to instantiate. Factory method design pattern lets a class defer instantiation to subclasses.

Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code.
They are an automatically generated class type.
When you reference a factory in a class constructor, Magento’s object manager generates the factory class if it does not exist. You can go through the following articles for more details:

https://www.mageplaza.com/magento-2-module-development/factory-object-magento-2.html

In Magento 2 all controller actions must return something, opposed to M1, where a controller action would just output something or did a redirect. The result of the execute method from each controller is generated in Magento\Framework\App\FrontController::dispatch() on line $result = $actionInstance->execute(); and returned.

Depending on the type of the returned result a different action is performed. The result can be and instance of :

\Magento\Framework\View\Result\Page - actually renders html \Magento\Framework\Controller\Result\Redirect - redirects to an other page \Magento\Framework\Controller\Result\Forward - forwards to an other action (internal redirect) \Magento\Framework\Controller\Result\Json - returns a json object. \Magento\Framework\Controller\Result\Raw - returns whatever you tell it to return (string).

Specific resultPageFactory is an instance of \Magento\Framework\View\Result\PageFactory and when calling create on that class it returns an instance of \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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top