Magento 2:从控制器返回JSON,XML,HTML和原始文本数据响应

本文说明了如何从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;
    }
}
?>

希望这可以帮助。谢谢。

版权属于: sbboke版权所有。

转载时必须以链接形式注明作者和原始出处及本声明。

张贴在magento2教程标签:

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论