按 frontName 路由,例如 magento2 中的 {frontName}/[params1]/[paramsX]...[?queryString]

routing by frontName like {frontName}/[params1]/[paramsX]...[?queryString] in magento2

Magento2 处理结构如下的请求:

{frontName}/{controller}/{action}

但我需要处理一些结构类似于

的请求

{frontName}/[params1]/[paramsX]...[?queryString]

最好的解决方法是什么? controller_front_send_response_before 的事件监听器? Magento\UrlRewrite\Controller\Route 的拦截器? 或任何其他...非常感谢

也许 .htaccess 重写?

感谢@RobbieAverill 的评论。 link 很有用。

我在 app/code/MyVendor/MyModule/etc/frontend 中创建了 di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<type name="Magento\Framework\App\RouterList">
    <arguments>
        <argument name="routerList" xsi:type="array">
            <item name="app_api_router" xsi:type="array">
                <item name="class" xsi:type="string">MyVendor\MyModule\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">10</item>
            </item>
        </argument>
    </arguments>
</type>

</config>

并在 app/code/MyVendor/MyModule/Controller/Router.php

中创建了 Router.php
<?php
namespace MyVendor\AppApi\Controller;

class Router implements \Magento\Framework\App\RouterInterface
{
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $identifier = trim($request->getPathInfo(), '/');
        if(strpos($identifier, 'routes.xml frontName') === 0) {
            // MY LOGIC CODE HERE
        }
    }
}