Magento Asked by Hoàng Thắng on February 11, 2021
I’m starting to learn Magento 2 and I read some instructions to create Controller.
There are the index.php. on Magento 2 _ development cookbook and another on website.
/** @var MagentoFrameworkViewResultPageFactory */
protected $resultPageFactory;
public function __construct( MagentoFrameworkAppActionContext $context,
MagentoFrameworkViewResultPageFactory
$resultPageFactory ){
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
return $resultPage;
}
and the another code that I read.
class Index extends Action
{
public function execute()
{
$this->_view->loadLayout();
$this->_view->renderLayout();`
I saw that, two codes are show same layout page Magento. So what does the different between that ?`
General
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 MagentoFrameworkAppFrontController::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 :
MagentoFrameworkViewResultPage - actually renders html
MagentoFrameworkControllerResultRedirect - redirects to an other page
MagentoFrameworkControllerResultForward - forwards to an other action (internal redirect)
MagentoFrameworkControllerResultJson - returns a json object.
MagentoFrameworkControllerResultRaw - returns whatever you tell it to return (string).
Specific
resultPageFactory
is an instance of MagentoFrameworkViewResultPageFactory
and when calling create on that class it returns an instance of MagentoFrameworkViewResultPage
described above.
When this is used and the result is returned it means that your action will return HTML.
It has somehow a similar effect as $this->loadLayout();
from Magento 1.
When you call create on the resultPageFactory object it actually loads the layout.
Answered by Hamendra Sunthwal on February 11, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP