本文说明了如何从Magento 2中的任何Controller类返回JSON数据,XML数据,HTML数据或原始文本数据。
通常,在Magento2控制器的execute()
功能中,我们会看到重定向或转发调用,该调用将重定向到指定的URL页面。
但是,有时我们可能需要返回JSON格式数据甚至原始文本数据。
在这里,我将展示如何实现这一目标。我假设模块名称为“ Chapagain_HelloWorld ”。
从Magento2控制器返回JSON数据
文件:app / code / Chapagain / HelloWorld / Controller / Index / Index.php
<?php
namespace Chapagain\HelloWorld\Controller\Index;
use Magento\Framework\UrlFactory;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @var \Magento\Framework\UrlFactory
*/
protected $urlFactory;
/**
* @var \Magento\Framework\Controller\Result\JsonFactory
*/
protected $resultJsonFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory resultPageFactory
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
UrlFactory $urlFactory
)
{
$this->resultPageFactory = $resultPageFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->urlModel = $urlFactory->create();
parent::__construct($context);
}
/**
* Example for returning JSON data
*
* @return string
*/
public function execute()
{
$result = $this->resultJsonFactory->create();
$data = [
'foo' =>'bar',
'success' => true
];
$result->setData($data);
return $result;
}
}
?>
输出:
{"foo":"bar","success":true}
从Magento2控制器返回XML数据
文件:app / code / Chapagain / HelloWorld / Controller / Index / Index.php
<?php
namespace Chapagain\HelloWorld\Controller\Index;
use Magento\Framework\UrlFactory;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @var \Magento\Framework\UrlFactory
*/
protected $urlFactory;
/**
* @var \Magento\Framework\Controller\Result\RawFactory
*/
protected $resultRawFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory resultPageFactory
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
UrlFactory $urlFactory
)
{
$this->resultPageFactory = $resultPageFactory;
$this->resultRawFactory = $resultRawFactory;
$this->urlModel = $urlFactory->create();
parent::__construct($context);
}
/**
* Example for returning Raw Text data
*
* @return string
*/
public function execute()
{
$result = $this->resultRawFactory->create();
// Return XML data
$result->setHeader('Content-Type', 'text/xml');
$result->setContents('<user>
<name>Mukesh Chapagain</name>
<country>Nepal</country>
</user>');
return $result;
}
}
?>
输出:
<user>
<name>Mukesh Chapagain</name>
<country>Nepal</country>
</user>
从Magento2控制器返回原始文本或HTML数据
文件:app / code / Chapagain / HelloWorld / Controller / Index / Index.php
<?php
namespace Chapagain\HelloWorld\Controller\Index;
use Magento\Framework\UrlFactory;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @var \Magento\Framework\UrlFactory
*/
protected $urlFactory;
/**
* @var \Magento\Framework\Controller\Result\RawFactory
*/
protected $resultRawFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory resultPageFactory
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
UrlFactory $urlFactory
)
{
$this->resultPageFactory = $resultPageFactory;
$this->resultRawFactory = $resultRawFactory;
$this->urlModel = $urlFactory->create();
parent::__construct($context);
}
/**
* Example for returning Raw Text data
*
* @return string
*/
public function execute()
{
$result = $this->resultRawFactory->create();
// Return Raw Text or HTML data
// $result->setContents('Hello World');
$result->setContents('<strong>Hello World</strong>');
return $result;
}
}
?>
希望这可以帮助。谢谢。