# Wie kann ich variablen aus der config im Subscriber Event Enlight\_Controller\_Action\_PreDispatch

**URL:** <https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418>\
**Category:** Programmierung\
**Created:** [4. August 2018 um 06:10 UTC](https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418 "2018-08-04T06:10:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielmauer](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@danielmauer](https://forum.shopware.com/u/danielmauer)\
**Post date:** [4. August 2018 um 06:10 UTC](https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418/1 "2018-08-04T06:10:40Z")

</div>

Hi all,

ich habe ein Plugin, welches mir eine Tracking ID und ein und einen Accesstoken aus der Plugin Config ausgeben soll.&nbsp;

Plugin Name: Zzz  
**ich habe folgende Struktur:**

Zzz.php  
&nbsp;

&nbsp;

plugin.xml  
&nbsp;

```
    Zzz
    Zzz

    1.0.0
    (c) by Zzz AG
    MIT
    http://Zzz.com
    Zzz AG
    

    
        Erstveröffentlichung
        First release

```

&nbsp;

Subscriber/TemplateRegistration.php  
&nbsp;

```
pluginDirectory = $pluginDirectory;
        $this->templateManager = $templateManager;
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return [
            'Enlight_Controller_Action_PreDispatch' => 'onPreDispatch'
        ];
    }

    public function onPreDispatch()
    {
        $this->templateManager->addTemplateDir($this->pluginDirectory . '/Resources/views');
    }
}

```

&nbsp;

Resources/services.xml  
&nbsp;

```
            access_token
            daypaio API Access Token
            daypaio API Access Token
            
            Tragen Sie hier den Access Token aus Ihrem daypaio Portal ein.
            Enter the access token from your daypaio portal here.
        
        
        
            tracking_id
            daypaio Tracking ID
            daypaio Tracking ID
            
            Tragen Sie hier die Tracking ID aus Ihrem daypaio Portal ein.
            Enter the tracking id from your daypaio portal here.
        

        
            linktrackingActivated
            Linktracking aktivieren
            Activate linktracking
            1

```

&nbsp;

views/frontend/index/header.tpl  
&nbsp;

```
{extends file="parent:frontend/index/header.tpl"}

{block name="frontend_index_header_javascript_tracking" append}

    {if !empty($tracking_id)}

    {literal}
        
            (function(w,d,sc,src,sy,scr,id){w['SYSObject']=sy;w[sy]=w[sy]||function(){
                (w[sy].q=w[sy].q||[]).push(arguments)},w[sy].l=1*new Date();scr=d.createElement(sc),
                id=d.getElementsByTagName(sc)[0];scr.async=0;scr.src=src;id.parentNode.insertBefore(scr,id)})
            (window,document,'script','https://data.getsystem.de/dc.js','sys');
            sys('init','{/literal}{$tracking_id}{literal}')
            {/literal} {if $linktrackingActivated == 1}
            {literal}sys('linktracking',true){/literal}
            {/if}{literal}
        
    {/literal}
    {/if}

    daniel

    Variable kam an und ist: {$danieltest}

{/block}

```

&nbsp;

So und nun möchte ich im Subscriber definieren das ich die Variablen aus der config an ein \*.tpl&nbsp;übergeben kann.

Mit&nbsp;  
&nbsp;

```
$tracking_id = Shopware()->Config()->get('tracking_id');
        $access_token = Shopware()->Config()->get('access_token');

```

bekomme ich zwar die Werte aus der Config wenn ich Sie mit echo ausgebe, aber ich kann diese keinem Template zuweisen.&nbsp;

Wenn ich&nbsp;

```
$this->View()->assign('dma', $tracking_id);
$this->View()->assign('danieltest', $access_token);

```

verwende, bekomme ich ich immer nur folgenden Fehler  
**Fatal error** : Uncaught Error: Call to a member function get() on null&nbsp;

&nbsp;

Kann mir jemand sagen wie ich im Subscriber die Variablen an ein Template übergeben kann?

Vielen Dank

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [4. August 2018 um 06:44 UTC](https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418/2 "2018-08-04T06:44:59Z")

</div>

Hallo danielmauer,

kannst du mal die gesamte Fehlermeldung inkl. Stacktrace posten?

Viele Grüße

---

<div class="post-metadata">

**Author:** ![danielmauer](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@danielmauer](https://forum.shopware.com/u/danielmauer)\
**Post date:** [4. August 2018 um 07:52 UTC](https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418/3 "2018-08-04T07:52:44Z")

</div>

> [@simkli schrieb:](https://forum.shopware.com/profile/16741/simkli "simkli")
> 
> Hallo danielmauer,
> 
> kannst du mal die gesamte Fehlermeldung inkl. Stacktrace posten?
> 
> Viele Grüße

klar,&nbsp;

```
Fatal error: Uncaught Error: Call to undefined method Zzz\Subscriber\TemplateRegistration::View() in /var/www/vhosts/daniel-mauer.de/shopware.daniel-mauer.de/custom/plugins/Zzz/Subscriber/TemplateRegistration.php:69 Stack trace: #0 /var/www/vhosts/daniel-mauer.de/shopware.daniel-mauer.de/engine/Library/Enlight/Event/Handler/Default.php(91): Zzz\Subscriber\TemplateRegistration->onPreDispatch(Object(Enlight_Controller_ActionEventArgs)) #1 /var/www/vhosts/daniel-mauer.de/shopware.daniel-mauer.de/custom/plugins/FroshProfiler/Components/Event/EventManager.php(292): Enlight_Event_Handler_Default->execute(Object(Enlight_Controller_ActionEventArgs)) #2 /var/www/vhosts/daniel-mauer.de/shopware.daniel-mauer.de/custom/plugins/FroshProfiler/Components/Event/EventManager.php(80): FroshProfiler\Components\Event\EventManager->parentNotify('Enlight_Control...', Object(Enlight_Controller_ActionEventArgs)) #3 /var/www/vhosts/daniel-mauer.de/shopware.daniel-mauer.de/engine/Library/Enlight/Controller/Action.php(171): FroshProfiler\Components in /var/www/vhosts/daniel-mauer.de/shopware.daniel-mauer.de/custom/plugins/Zzz/Subscriber/TemplateRegistration.php on line 69

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [4. August 2018 um 08:10 UTC](https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418/4 "2018-08-04T08:10:25Z")

</div>

Ah, jetzt steht da plötzlich _„Call to undefined method Zzz\Subscriber\TemplateRegistration::View()“_ anstelle von _" **Fatal error** : Uncaught Error: Call to a member function get() on null"_ wie du zuvor oben geschrieben hast. ![Sticking-out-tongue](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/sticking-out-tongue.png "Sticking-out-tongue")

Die neue Fehlermeldung lässt sich wie folgt lösen: Du kannst mit $this-\>View() nicht auf den View zugreifen. Eine Möglichkeit wird hier gezeigt. Hole dir eine Referenz zum Controller. Dieser wird über die Event-Arguments übergeben:

[https://developers.shopware.com/developers-guide/plugin-quick-start/#subscriber-classes](https://developers.shopware.com/developers-guide/plugin-quick-start/#subscriber-classes)

Dann weißt du deine Variable via $view-\>assign(…) zu.

&nbsp;

Alternativ kannst du deinem Subscriber via der service.xml auch eine Referenz zum Template-Manager mitgeben. Dies wird hier ganz schön beschrieben:  
[Example plugin: storefront extension](https://developers.shopware.com/developers-guide/example-plugin/#register-template-first)

&nbsp;

Viele Grüße

---

<div class="post-metadata">

**Author:** ![danielmauer](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@danielmauer](https://forum.shopware.com/u/danielmauer)\
**Post date:** [6. August 2018 um 05:42 UTC](https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418/5 "2018-08-06T05:42:31Z")

</div>

noch eine kurze Frage, das funktioniert alle in der onPostDispatch aber nicht in der preDispatch.

&nbsp;

hier mal die Subscriber.php

```
pluginDirectory = $pluginDirectory;
        $this->templateManager = $templateManager;

        //$this->pluginName = $pluginName;

        //$this->config = $configReader->getByPluginName($pluginName);

    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return [
            'Enlight_Controller_Action_PreDispatch' => 'onPreDispatch',
            'Enlight_Controller_Action_PostDispatchSecure_Frontend' => 'onPostDispatch'
        ];
    }

    public function onPreDispatch(\Enlight_Event_EventArgs $args)
    {
        $this->templateManager->addTemplateDir($this->pluginDirectory . '/Resources/views');
        //echo 'daypaio';
        //$shop = Shopware()->Shop();
        $tracking_id = Shopware()->Config()->get('tracking_id');
        $access_token = Shopware()->Config()->get('access_token');

        //echo $tracking_id . '';
        //echo $access_token . '';

        /** @var \Enlight_Controller_Action $controller */
        //$controller = $args->get('subject');
        //$view = $controller->View();

        //echo 'View: ' . $view;

        //$pluginConfig = $this->container->get('shopware.plugin.cached_config_reader')->getByPluginName('Zzz');
        //$trackingId = $pluginConfig['tracking_id'];

        //$controller->View()->assign('tracking_id', $trackingId);

        //echo $controller;
        //$this->View()->assign('dma', $tracking_id);
        //$this->View()->assign('danieltest', $access_token);

    }

    public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args)
    {
        /** @var \Enlight_Controller_Action $controller */
        $controller = $args->get('subject');
        $view = $controller->View();

        $view->addTemplateDir($this->pluginDirectory . '/Resources/views');

        $tracking_id = Shopware()->Config()->get('tracking_id');
        $access_token = Shopware()->Config()->get('access_token');
        $linkTrackingStatus = Shopware()->Config()->get('linktrackingActivated');

        $view->assign('tracking_id', $tracking_id);
        $view->assign('access_token', $access_token);
        $view->assign('linktrackingActivated', $linkTrackingStatus);
/*
        $view->assign('swagSloganFontSize', $this->config['swagSloganFontSize']);
        $view->assign('swagSloganItalic', $this->config['swagSloganItalic']);
        $view->assign('swagSloganContent', $this->config['swagSloganContent']);

        if (!$this->config['swagSloganContent']) {
            $view->assign('swagSloganContent', $this->sloganPrinter->getSlogan());
        }
*/
    }
}

```

&nbsp;

kannst du mir noch erklären wie ich über die onPreDispatch realisieren kann?&nbsp;  
Vielen Dank

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [6. August 2018 um 05:51 UTC](https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418/6 "2018-08-06T05:51:14Z")

</div>

Das geht schon. Aber du hast eben das allgemeine PreDispatch-Event abonniert. Das wird auch bei der API und denn Backend aufgerufen. Die API hat gar kein Template also kann das nicht gut gehen.

Du musst wie beim PostDispatch-Event ein \_Frontend anhängen.

Viele Grüße

edit: ein PreDispatchSecure gibt es übrigens nicht. Also nur …PreDispatch\_Frontend

Ausführlich erklärt wird das Ganze hier:&nbsp;[https://developers.shopware.com/developers-guide/event-guide/](https://developers.shopware.com/developers-guide/event-guide/)

---

<div class="post-metadata">

**Author:** ![danielmauer](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@danielmauer](https://forum.shopware.com/u/danielmauer)\
**Post date:** [6. August 2018 um 08:00 UTC](https://forum.shopware.com/t/wie-kann-ich-variablen-aus-der-config-im-subscriber-event-enlight-controller-action-predispatch/54418/7 "2018-08-06T08:00:06Z")

</div>

super, vielen dank für die Info.&nbsp;
