I am calling an controller function via Ajax and its calling and returning correct data. But how can I return Controller function respective phtml file content instead of some data from controller function.
Here is my current code that returning data -
public function execute()
{
$result = $this->resultJsonFactory->create();
$request = $this->getRequest();
if ($request->isAjax()) {
$method = $request->getParam('method');
if($method != '')
$regions = $this->getRegionByCountryId($request);
$result->setData($regions);
} else {
$result->setData(['error' => 'method not found']);
}
}
return $result;
}
Its running fine and retruning data to my ajax function.
But I want to return phtml file content instead of this data, So for that I have created an handler for this and respective phtml file.
Handler file is - booking_index_ajax.xml with below code -
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="main" htmlClass="content-page">
<block class="Mymodule\Booking\Block\BookingList"
name="test.booking.filtertable"
template="Mymodule_Booking::content/test.phtml"
after="-"/>
</referenceContainer>
</body>
</page>
And created an respective test.phtml file on specified location -
<?php
echo "Hello";
?>
But my test.phtml file content is not retruning, Please let me know what I am doing wrong.
You answer worked #AMIT, But I am trying to use my execute function to get Json and phtml file both - Its returning phtml if I am not applying 'return' in Json If block, But If I am applying "return $result" statement in then returning nothing. here is mine code -
public function execute()
{
$request = $this->getRequest();
if ($request->isAjax()) {
$method = $request->getParam('method');
if ($method === 'getRegionByCountryId') {
$result = $this->resultJsonFactory->create();
$regions = $this->getRegionByCountryId($request);
$result->setData($regions);
//return $result;
} elseif ($method === 'getAddressesByCountryId') {
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
return $resultLayout;
} else {
$result = $this->resultJsonFactory->create();
$result->setData(['error' => 'method not found']);
return $result;
}
}
}
Regards
2 Answers 2
You need to do some modify at controller and layout file:
Action file look like:
namespace{YourClassNameSpace};
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultFactory;
class {YourClassname} extends \Magento\Framework\App\Action\Action
{
............
..............
public function execute()
{
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
return $resultLayout;
}
}
Now,you have add root container at your layout file booking_index_ajax.xml :
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<container name="root">
<block class="Mymodule\Booking\Block\BookingList"
name="test.booking.filtertable"
template="Mymodule_Booking::content/test.phtml"
/>
</container>
</layout>
-
But I am doing something like - using same function to return Json data and our phtml file but its not returning Json - here is mine function - I have updated my execute function code at the end of my question - Plz checkAtul– Atul2018年08月03日 05:58:03 +00:00Commented Aug 3, 2018 at 5:58
-
#amit Please check at the end of my question......Atul– Atul2018年08月03日 06:05:58 +00:00Commented Aug 3, 2018 at 6:05
-
semms ok, accept this
//return $result;i think you should un-comment this code2018年08月03日 06:24:41 +00:00Commented Aug 3, 2018 at 6:24 -
It works, error was something else....Thank you :)Atul– Atul2018年08月03日 07:19:17 +00:00Commented Aug 3, 2018 at 7:19
-
How we can upload specific js file in the AJax phtml file ?Atul– Atul2018年08月03日 10:33:08 +00:00Commented Aug 3, 2018 at 10:33
You can render phtml in your controller with following code.
$this->_view->loadLayout();
$this->_view->getLayout()
->createBlock("Mymodule\Booking\Block\BookingList")
->setTemplate("Mymodule_Booking::content/test.phtml")
->toHtml();