首先,为模块定义路径。这是完成的<module_directory>/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="timestwo" frontName="timestwo">
<module name="Your_Module"/>
</route>
</router>
</config>
然后为此路线制作“索引”控制器 <module_directory>/Controller/Index.php
<?php
namespace Your\Module\Controller;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
protected $resultPageFactory;
public function __construct(Context $context, PageFactory $pageFactory)
{
$this->resultPageFactory = $pageFactory;
parent::__construct($context);
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
return $resultPage;
}
}
为此页面制作布局文件 <module_directory>/view/frontend/layout/timestwo_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>Form</title>
</head>
<body>
<referenceContainer name="content">
<block name="form" class="Magento\Framework\View\Element\Template" template="Your_Module::form.phtml"/>
</referenceContainer>
</body>
</page>
现在制作模板 <module_directory>/view/frontend/templates/form.phtml
<form class="form" action="result" method="post">
<fieldset class="fieldset">
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Test Form') ?></span></legend><br>
<div class="field required">
<label for="number" class="label">
<span><?php /* @escapeNotVerified */ echo __('Input') ?></span>
</label>
<div class="control">
<input type="text" id="number"
name="number"
class="input-text">
</div>
</div>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit" class="action submit primary" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>">
<span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
</button>
</div>
</div>
</form>
现在为结果页面创建控制器: <module_directory>/Controller/Result.php
<?php
namespace Your\Module\Controller;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Element\Messages;
use Magento\Framework\View\Result\PageFactory;
class Result extends Action
{
/** @var PageFactory $resultPageFactory */
protected $resultPageFactory;
/**
* Result constructor.
* @param Context $context
* @param PageFactory $pageFactory
*/
public function __construct(Context $context, PageFactory $pageFactory)
{
$this->resultPageFactory = $pageFactory;
parent::__construct($context);
}
/**
* The controller action
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
$number = $this->getRequest()->getParam('number');
$resultPage = $this->resultPageFactory->create();
/** @var Messages $messageBlock */
$messageBlock = $resultPage->getLayout()->createBlock(
'Magento\Framework\View\Element\Messages',
'answer'
);
if (is_numeric($number)) {
$messageBlock->addSuccess($number . ' times 2 is ' . ($number * 2));
}else{
$messageBlock->addError('You didn\'t enter a number!');
}
$resultPage->getLayout()->setChild(
'content',
$messageBlock->getNameInLayout(),
'answer_alias'
);
return $resultPage;
}
}
最后,为结果页面制作布局xml <module_directory>/view/frontend/layout/timestwo_result.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>Result</title>
</head>
</page>
运行php bin/magento setup:upgrade
以启用您的模块(如果尚未启用)。现在清除缓存。转到<magento_location>/timestwo
浏览器中查看表单