1

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

asked Aug 3, 2018 at 4:41

2 Answers 2

3

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>
answered Aug 3, 2018 at 5:04
7
  • 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 check Commented Aug 3, 2018 at 5:58
  • #amit Please check at the end of my question...... Commented Aug 3, 2018 at 6:05
  • semms ok, accept this //return $result; i think you should un-comment this code Commented Aug 3, 2018 at 6:24
  • It works, error was something else....Thank you :) Commented Aug 3, 2018 at 7:19
  • How we can upload specific js file in the AJax phtml file ? Commented Aug 3, 2018 at 10:33
0

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();
answered Aug 3, 2018 at 5:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.