0

I want my front controller to serve an xml type and for that reason i need that controller to use a layout that doesn't include any html tags.

On my controller class i have the following code:

<?php
namespace Vendor\Module\Controller\Feed;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action
{
 protected $pageFactory;
 public function __construct(Context $context, PageFactory $pageFactory)
 {
 $this->pageFactory = $pageFactory;
 return parent::__construct($context);
 }
 public function execute()
 { 
 // $this->getResponse()->setHeader('Content-Type','text/xml');
 $page_object = $this->pageFactory->create();
 return $page_object;
 } 
}

The pageFactory object seems to be including the theme's base layout. Overriding the <container name="root"> node on this controller-action layout, doesn't stop it from rendering head and body tags.

Is there a different object i should be using in the execute() function? What would be the correct way of getting the front controller to use an empty layout?

sv3n
11.7k7 gold badges44 silver badges75 bronze badges
asked Jun 7, 2016 at 17:16

1 Answer 1

1

I assume you do not actualy need an empty result, but JSON or XML result.

For the JSON result you can inject \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory into your constructor. Then you are free to pass the response like this:

public function execute()
{
 $result = $this->resultJsonFactory->create();
 return $result->setData(['success' => true]);
}

RAW result: inject Magento\Framework\Controller\Result\RawFactory $rawResultFactory

public function execute()
{
 $result = $this->rawResultFactory->create();
 $result->setHeader('Content-Type', 'text/xml');
 $result->setContents('&lt;root>&lt;science>&lt;/science>&lt;/root>');
 return $result;
}

Please refer to Alan Storm's quickie for details http://magento-quickies.alanstorm.com/post/141260832260/magento-2-controller-result-objects

sv3n
11.7k7 gold badges44 silver badges75 bronze badges
answered Jun 8, 2016 at 8:40

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.