What's best practice for an ajax request which involves - receiving some params in a request, loading data based on these params, processing this data, generating html and sending the html back in a json response?
For instance, say I have a product id that I pass as a param, I have to return html for related product listing html as a json response.
In M1 I would create a controller action and do the following:
$product = Mage::getModel('catalog/product')
$this->loadLayout();
$html = $this->getLayout()->getBlock('mycustom_module/block')- >setProduct($product)->toHtml();
$response('html' => $html);
$this->getResponse()->setBody($response);
$this->getResponse()->setHeader('Content Type: json');
$this->sendResponse();
return $this;
In my custom module's block class I would basically load the collection of related products for the product and form the html in the template and return the html output in the json. How do I go about doing this sort of a thing with M2?
Doing a similar thing as M1 while making sure I use best practices like using DI instead of object manager is good enough? Or would you design this sort of functionality differently?