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();
  • 2
    Pagefactory is used to initialize the layout. Sep 5 '18 at 6:12

Pagefactory is used to initialize the layout.

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


  • Thank you sir, and could you help me one more time. what is postfactory. Sep 5 '18 at 7:26


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 an instance of:

\Magento\Framework\View\Result\Page - actually renders HTML \Magento\Framework\Controller\Result\Redirect - redirects to another page \Magento\Framework\Controller\Result\Forward - forwards to another 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.


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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.