Closed Linnzh closed 6 months ago
以下是在 Magento 2 模块 Example_Started 中编写 RESTful 控制器并返回特定信息的详细步骤:
创建模块:
在 Magento 2 项目的 app/code
目录下创建名为 Example/Started
的模块。你可以使用 Magento 2 的命令行工具创建模块,也可以手动创建。
创建 JSON 控制器:
在模块目录下创建 Controller/Json/Index.php
文件,并将以下代码添加到文件中:
<?php
namespace Example\Started\Controller\Json;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
class Index extends Action
{
protected $resultJsonFactory;
public function __construct(
Context $context,
JsonFactory $resultJsonFactory
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
}
public function execute()
{
$result = $this->resultJsonFactory->create();
$data = ['message' => 'Hello, Magento'];
return $result->setData($data);
}
}
创建文本字符串控制器:
在模块目录下创建 Controller/Text/Index.php
文件,并将以下代码添加到文件中:
<?php
namespace Example\Started\Controller\Text;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
class Index extends Action
{
public function execute()
{
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
return $result->setContents('Hello, Magento');
}
}
创建 HTML 视图页面控制器:
在模块目录下创建 Controller/Example/View.php
文件,并将以下代码添加到文件中:
<?php
namespace Example\Started\Controller\Example;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\PageFactory;
class View implements HttpGetActionInterface
{
private PageFactory $pageFactory;
private ResultFactory $resultFactory;
public function __construct(PageFactory $pageFactory, ResultFactory $resultFactory)
{
$this->pageFactory = $pageFactory;
$this->resultFactory = $resultFactory;
}
/**
* @inheritDoc
*
* @link http://start.kyoye.com/started/example/view
*/
public function execute()
{
// 它默认输出当前模块下 `view/frontend/layout`目录下与 Controller 同样位置的布局。
// 拿这里的`Example\Started\Controller\Example\Example`类来说,它可以对应`view/frontend/layout/example/view.xml` 布局文件
// return $this->pageFactory->create();
return $this->pageFactory->create()->addHandle('started_example_view2');
}
}
etc/frontend/routes.xml
文件中添加以下内容:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="started" frontName="example_started">
<module name="Example_Started" />
</route>
</router>
</config>
6.关联html的layout页面 在Example/Started/view/frontend/layout下创建started_example_view2.xml,添加以下内容:
<?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="content">
<block name="example.started.view" class="Magento\Framework\View\Element\Template" template="Example_Started::view.phtml" />
</referenceContainer>
</body>
</page>
7.创建view/frontend/layout/example/view.xml
布局文件
清理缓存: 在命令行中进入 Magento 2 项目的根目录,并执行以下命令来清理缓存:
php bin/magento cache:clean
访问控制器: 在浏览器中访问以下 URL,查看控制器返回的结果:
http://start.kyoye.com/started/json
http://start.kyoye.com/started/Text
http://start.kyoye.com/started/example/view
Json:返回一个 JSON 对象
Text:返回一个Hello, Magento
字符串
View:返回一个 HTML 视图页面,包含一个标题为Hello, Magento
Hello, Magento
字符串<h1>
标题为Hello, Magento
提供代码和运行截图。
每个任务使用三级标题区分,代码需要使用代码块展示。
相关代码的变更需要关联本 issue。