1

我正在尝试frontend/home/index.tpl通过自己的插件扩展模板。

这是我的根文件:

<?php
namespace TdevExtend;
use Shopware\Components\Plugin;
class TdevExtend extends Plugin
{
   public static function getSubscribedEvents(){
    return [
        'Enlight_Controller_Dispatcher_ControllerPath_Frontend_MyPlugin' =>
            'onDetailPostDispatch'
    ];
   }
   public function onDetailPostDispatch(\Enlight_Event_EventArgs $args)
   {
      $this->container->get('template')->addTemplateDir(
        $this->getPath() . '/Resources/views/'
      );
    return __DIR__ . '/Controllers/Frontend/MyPlugin.php';
   }
}
?>

这是我的控制器MyPlugin.php在目录 Controllers\Frontend

public function preDispatch()
   {
    /** @var \Shopware\Components\Plugin $plugin */
    $plugin = $this->get('kernel')->getPlugins()['TdevProductTab'];

    $this->get('template')->addTemplateDir($plugin->getPath() . '/Resources/views/');
}

和文件夹 Resources\views\frontend\home\ 中的模板

 {extends file="parent:frontend/home/index.tpl"}
 {block name='frontend_index_content'}
    <div class="">Hello world!</div>
    {$smarty.block.parent}
 {/block}

我已经阅读了官方文档并研究了插件示例插件示例 在后端安装/重新安装插件后创建缓存并删除var/cache. 但没有什么能帮助我。

4

1 回答 1

3

您可能希望Enlight_Controller_Action_PostDispatchSecure_Frontend_Index在 getSubscribedEvents 中使用 Event 而不是Enlight_Controller_Dispatcher_ControllerPath_Frontend_MyPlugin.

您正在使用 Event Enlight_Controller_Dispatcher_ControllerPath_Frontend_MyPlugin。在寻找控制器时MyPluginShopware 将创建此事件。所以在使用这个事件的时候需要自己编写Controller。但我猜你想要的是上面提到的事件。实际上,您不需要编写控制器。

<?php
namespace TdevExtend;
use Shopware\Components\Plugin;
class TdevExtend extends Plugin
{
   public static function getSubscribedEvents(){
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Index' =>
            'onPostDispatch'
    ];
   }
   public function onPostDispatch(\Enlight_Event_EventArgs $args)
   {
      $this->container->get('Template')->addTemplateDir(
        $this->getPath() . '/Resources/views/'
      );
   }
}
?>
于 2017-03-16T06:01:30.503 回答