1. controller responsibility
2.创建一个模块
2.1 配置 module.xml 文件:
app/code/Test/First/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Test_First" setup_version="2.0.0"/>
</config>
2.2 在 config.php 中添加
app/etc/config.php
'Test_First' => 1,
2.3 注册 registration.php
app/code/Test/First/registration.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Test_First',
__DIR__
);
运行命令。
3.创建控制器
3.1创建配置文件
app/code/Test/First/etc/frontend/routes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="first" frontName="first">
<module name="Test_First"/>
</route>
</router>
</config>
3.2 创建控制器文件
<?php
namespace Test\First\Controller\Hello;
class World extends \Magento\Framework\App\Action\Action
{
public function execute()
{
echo "<p>wwww</p>";
var_dump(__METHOD__);
}
}
<?php
namespace Test\First\Controller\Hello;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class World extends \Magento\Framework\App\Action\Action
{
protected $pageFactory;
public function __construct(Context $context,PageFactory $pageFactory)
{
$this->pageFactory = $pageFactory;
parent::__construct($context);
}
public function execute()
{
var_dump(__METHOD__);
$page_object = $this->pageFactory->create();
return $page_object;
}
}
4.创建视图
4.1创建布局文件
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1columns" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="content">
<block
template="content.phtml"
class="Test\First\Block\Main"
name="test_hello_world"/>
</referenceBlock>
</body>
</page>
//注意布局,1columns显示不了
4.2创建 block
<?php
namespace Test\First\Block;
use Magento\Framework\View\Element\Template;
class Main extends Template
{
protected function _prepareLayout()
{
} //这一段可有可无
}
4.3创建 phtml
<h1>
hello,world !
</h1>