问题
I'm trying to extend template frontend/home/index.tpl
via own plugin.
Here my root file:
<?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';
}
}
?>
Here are my Controller MyPlugin.php in dirrectory Controllers\Frontend
public function preDispatch()
{
/** @var \Shopware\Components\Plugin $plugin */
$plugin = $this->get('kernel')->getPlugins()['TdevProductTab'];
$this->get('template')->addTemplateDir($plugin->getPath() . '/Resources/views/');
}
And template in folder 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}
I have read official documentation and researched plugins examples plugins examples
After installing/reinstalling plugin crear cache on backend and delete manualy folder in var/cache
. But nothing has helped me.
回答1:
You may want to use the Event Enlight_Controller_Action_PostDispatchSecure_Frontend_Index
in getSubscribedEvents instead of Enlight_Controller_Dispatcher_ControllerPath_Frontend_MyPlugin
.
You are using the Event Enlight_Controller_Dispatcher_ControllerPath_Frontend_MyPlugin
. When looking for controller MyPlugin
Shopware will create this Event. So you need to write your own Controller when using this event. But I guess what you want is the event mentioned above. Actually you don't need to write a 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/'
);
}
}
?>
来源:https://stackoverflow.com/questions/42819695/shopware-5-2-20-cannot-extend-template-via-plugin