创建基础前端路由web/routing/index/index

magento 收藏
0 33

在Vendor/Module/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="routing" frontName="routing">
<module name="Vendor_Module" />

手动补齐结束标签


在Vendor/Module/Controller/Index/Index.php

<?php
declare(strict_types=1);

namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;

/**
* Class Index
*/
class Index implements HttpGetActionInterface
{
/**
* @var PageFactory
*/
private $pageFactory;

/**
* @var RequestInterface
*/
private $request;

/**
* @param PageFactory $pageFactory
* @param RequestInterface $request
*/
public function __construct(PageFactory $pageFactory, RequestInterface $request)
{
$this->pageFactory = $pageFactory;
$this->request = $request;
}

/**
* @inheritdoc
*/
public function execute()
{
// Get the params that were passed from our Router
// $firstParam = $this->request->getParam('first_param', null); //如果需要参数
// $secondParam = $this->request->getParam('second_param', null); //如果需要参数

return $this->pageFactory->create();
}
}

创建新路由后routing/index/index,最好为管理员提供更多控制权。通过创建新Page Type的 ,管理员可以使用小部件管理此页面的内容。

<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page_types xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_types.xsd">
<type id="routing_index_index" label="custom router test page"/>

手动补齐结束标签



    暂时没有人评论