Answers:
假设您具有以下模块Company/Module。
/ app / code / Company / Module / etc / frontend / routes.xml
创建管理路线:
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="companymodule" frontName="companymodule">
            <module name="Company_Module"/>
        </route>
    </router>
</config>/ app / code / Company / Module / view / frontend / layout / module_index_booking.xml
创建一个基本布局,将“块”关联到表单页面phtml模板
<?xml version="1.0"?>
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>HTML title - The booking form page</title>
    </head>
    <body>
        <referenceBlock name="navigation.sections" remove="true" />
        <referenceContainer name="content">
            <block class="Company\Module\Block\Booking" name="companymodule.booking" template="Company_Module::booking.phtml"/>
        </referenceContainer>
    </body>
</page>/ app / code / Company / Module / Block / Booking.php
创建一个具有许多您想要的表单功能的块。
<?php
namespace Company\Module\Block;
class Booking extends \Magento\Framework\View\Element\Template
{
    /**
     * Construct
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template $context,
        array $data = []
    )
    {
        parent::__construct($context, $data);
       }
    /**
     * Get form action URL for POST booking request
     *
     * @return string
     */
    public function getFormAction()
    {
            // companymodule is given in routes.xml
            // controller_name is folder name inside controller folder
            // action is php file name inside above controller_name folder
        return '/companymodule/controller_name/action';
        // here controller_name is index, action is booking
    }
}/ app / code / Company / Module / view / frontend / templates / booking.phtml
使用您的HTML表单创建一个模板,并添加与路由相对应的表单操作。
<h1>Booking page</h1>
<form action="<?php echo $block->getFormAction() ?>" method="post">
    <input name="firstname" type="text">
    <input name="lastname" type="text">
    <input name="phone" type="text">
    <input name="bookingTime" type="date">
    <input type="submit" value="Send booking informations">
</form>/ app / code / Company / Module / Controller / Index / Booking.php
创建一个动作控制器来管理路径上的请求。
<?php
namespace Company\Module\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Booking extends \Magento\Framework\App\Action\Action
{
    /**
     * Booking action
     *
     * @return void
     */
    public function execute()
    {
        // 1. POST request : Get booking data
        $post = (array) $this->getRequest()->getPost();
        if (!empty($post)) {
            // Retrieve your form data
            $firstname   = $post['firstname'];
            $lastname    = $post['lastname'];
            $phone       = $post['phone'];
            $bookingTime = $post['bookingTime'];
            // Doing-something with...
            // Display the succes form validation message
            $this->messageManager->addSuccessMessage('Booking done !');
            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/companymodule/index/booking');
            return $resultRedirect;
        }
        // 2. GET request : Render the booking page 
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}在恢复时,您将具有以下架构:
应用程式 ├代码 | ├公司 | | ├模块 | | | ├座 | | | | ├Booking.php | | | ├控制器 | | | | ├索引 | | | | | ├Booking.php | | | ├等 | | | | ├前端 | | | | | ├routes.xml | | | ├查看 | | | | ├前端 | | | | | ├布局 | | | | | | ├module_index_booking.xml | | | | | ├模板 | | | | | | ├booking.phtml
然后运行以下命令:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush然后,您可以访问自定义表单页面:http:// localhost / companymodule / index / booking
编码愉快!