2

我想覆盖现有Magento/*模块的控制器行为。我想创建自己的Magento/Customer/Controller/Account/LoginPost.php实现。

  1. 我怎样才能做到这一点?
  2. 依赖注入对于模型类来说似乎是件好事,但控制器呢?我可以在某处注入我自己的 LoginPost 控制器类,以便某些对象使用我自己的实现吗?
4

2 回答 2

6

您可以为此使用Magento2 的插件功能。

Magento 使您能够更改或扩展任何 Magento 类中任何原始公共方法的行为。您可以通过创建扩展来更改原始方法的行为。这些扩展使用Plugin该类,因此被称为插件。

app/code/YourNamespace/YourModule/etc/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\Customer\Controller\Account\LoginPost">
       <plugin name="yourModuleAccountLoginPost" type="YourNamespace\YourModule\Plugin\Customer\LoginPost" sortOrder="10" disabled="false"/>
   </type>
</config>

创建一个名为的新文件app/code/YourNamespace/YourModule/Plugin/Customer/LoginPost.php并在其中写入以下代码:

<?php

    namespace YourNamespace\YourModule\Plugin\Customer;

    class LoginPost
    {
        public function aroundExecute(\Magento\Customer\Controller\Account\LoginPost $subject, \Closure $proceed)
        {
            // your custom code before the original execute function
            $this->doSomethingBeforeExecute();

            // call the original execute function
            $returnValue = $proceed();

            // your custom code after the original execute function
            if ($returnValue) {
                $this->doSomethingAfterExecute();
            }

            return $returnValue;
        }
    }
?>

同样,您也可以在上面的类中使用beforeExecute()&afterExecute()函数。请查看此链接了解详细信息。

于 2016-05-15T03:41:26.947 回答
3

经过一番调查,我找到了解决方案;-)。

该资源非常有帮助:https ://github.com/tzyganu/Magento2SampleModule 。

此解决方案的示例模块在这里: https ://github.com/nuclearhead/M2OverrideAction

效果是,如果你转到 URI /customer/account/login,来自自定义模块的方法将被触发,而不是来自Magento_Customer模块的默认方法,并且 URL 将保持不变。当然,你也可以对loginPost动作做同样的事情。

Routerdi.xml. 我简化了 tzyganu 的 SampleNews 模块版本以阐明解决方案。Router类检查返回方法的 URI $request->getPathInfo(),然后将新配置设置为$request

$request->setModuleName('overrideaction')
      ->setControllerName('view')
      ->setActionName('index');
$request->setDispatched(true);
$this->dispatched = true;
return $this->actionFactory->create(
    'Magento\Framework\App\Action\Forward',
    ['request' => $request]
);

我的etc/frontend/di.xml自定义模块:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\RouterList">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="customer" xsi:type="array">
                    <item name="class" xsi:type="string">MiniSamples\OverrideAction\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">9</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>
于 2015-04-22T06:42:27.577 回答