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:


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?

1 Answer 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');
    return $result;

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.