【问题标题】:Magento Multiple Dynamic Custom Routes For Single Store单个商店的 Magento 多个动态自定义路由
【发布时间】:2018-12-17 08:16:12
【问题描述】:

我想加载我的 magento 商店 www.example.com/var1/var2 而不是 www.example.com,我想要我的产品网址,例如 www.example.com/var1/var2/product-url Var1 & var2 可以是动态变量。 帮我在 maganeto 2.2.6 中重写我的网址

我尝试的是开发一个自定义模块,但现在我要做的就是在我的自定义模块中加载所有 magento 模块。似乎在 magento 中完全自定义,这不是一个好习惯。通过这种方式,我必须将所有 magento 模块重新初始化到我的自定义模块中。通过这种方式,使用这种方法,magento 对我来说毫无用处。

【问题讨论】:

标签: magento routes magento2 router magento2.2


【解决方案1】:

在这种情况下,您可以通过执行以下步骤来使用请求路由:

  1. 在 YourVendor/YourModule/etc/di.xml 下创建 di.xml 并包含以下内容:

<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="custom_router" xsi:type="array"> <item name="class" xsi:type="string">YourVendor\YourModule\Controller\Router </item> <item name="disable" xsi:type="boolean">false</item> <item name="sortOrder" xsi:type="string">70</item> </item> </argument> </arguments> </type> </config>

  1. 创建实现RouterInterface的Router类,如下:

    类路由器实现\Magento\Framework\App\RouterInterface { 私人 $actionFactory;

    /**
     * Router constructor.
     * @param \Magento\Framework\App\ActionFactory $actionFactory
     */
    
    public function __construct(\Magento\Framework\App\ActionFactory $actionFactory)
    {
        $this->actionFactory = $actionFactory;
    }
    
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $info = $request->getPathInfo();
        if (preg_match("%^/(var1/var2)(.*?)$%", $info, $m)) {
            $request->setPathInfo(str_replace('var1/var2', '', $info));
            return $this->actionFactory->create('Magento\Framework\App\Action\Forward',
                ['request' => $request]);
        }
        return null;
    }
    

    }

  2. 运行这个命令:

    php bin/magento s:up

输入您的网址,例如 www.example.com/var1/var2 并查看结果

【讨论】:

  • 我已经尝试过这个并且它已经工作了。但它只加载主页。我希望我的所有网址(如我的产品 href 和购物车页面链接)都应转换为 www.example.com/var1/var2/my-cart 或 www.example.com/var1/var2/my-product
  • 我不想更改 base_url,因为 var1 和 var2 是动态的,我不想为每个具有不同 var1/var2 的用户一次又一次地更新数据库
  • var1/var2 基本上是 var1 是类型,var2 是供应商名称,所以两者都可以一次又一次地更改
猜你喜欢
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多