I want replacement of following in magento 2.x
$block = $this->getLayout()->createBlock('customer/form_login')->setTemplate('persistent/customer/form/login.phtml');
$this->getResponse()->setBody($block->toHtml());
 asked Apr 2, 2016 at 7:27
 
 
 
 Manish 
 
 3,1447 gold badges33 silver badges48 bronze badges
 
 1 Answer 1
You can try following way to create new block inside controller
<?php
namespace [Vendor]\[Module]\Controller\[ControllerName];
use Magento\Framework\App\Action\Context;
class [YourControllerAction] extends \Magento\Framework\App\Action\Action
{
 /**
 * Index constructor.
 *
 * @param Context $context
 */
 public function __construct(
 Context $context
 ) {
 parent::__construct($context);
 }
 /**
 * @return 
 */
 public function execute()
 {
 $block = $this->_view->getLayout()
 ->createBlock('Magento\Customer\Block\Form\Login')
 ->setTemplate('Magento_Customer::form/login.phtml')
 ->toHtml();
 $this->getResponse()->setBody($block);
 }
}
 Aditya Yadav
 
 1662 silver badges16 bronze badges
 
 
 answered Apr 2, 2016 at 8:02
 
 
 
 Sohel Rana 
 
 36.2k3 gold badges74 silver badges94 bronze badges
 
 - 
 this statement not get execute I am checking to print log before and after this statement. $block = $resultPage->getLayout() ->createBlock('Magento\Customer\Block\Form\Login') ->setTemplate('Magento_Customer::form/login.phtml') ->toHtml();Manish– Manish2016年04月02日 08:17:12 +00:00Commented Apr 2, 2016 at 8:17
- 
 Do you initialize $this->resultPageFactory by construct?Sohel Rana– Sohel Rana2016年04月02日 08:20:47 +00:00Commented Apr 2, 2016 at 8:20
- 
 check updated codeSohel Rana– Sohel Rana2016年04月02日 08:29:01 +00:00Commented Apr 2, 2016 at 8:29
- 
 3@Manish: whenever you change the __construct function, you need to remove /var/generation folder so Magento 2 can re-created the cache code0xh8h– 0xh8h2016年10月11日 04:04:33 +00:00Commented Oct 11, 2016 at 4:04
- 
 1ERROR: $this->_view->getLayout()->getLayout() Pleas update :) All other line work fine. ThanksPatryk– Patryk2018年08月31日 17:32:21 +00:00Commented Aug 31, 2018 at 17:32
Explore related questions
See similar questions with these tags.
default