# Datenaustausch zwischen Controller und .tpl

**URL:** <https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786>\
**Category:** Programmierung\
**Created:** [24. August 2017 um 07:31 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786 "2017-08-24T07:31:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoppler](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@hoppler](https://forum.shopware.com/u/hoppler)\
**Post date:** [24. August 2017 um 07:31 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/1 "2017-08-24T07:31:48Z")

</div>

Hallo liebe Shopware Community,

ich benötige Eure Hilfe.

Mit Erfolg habe ich nach Anweisung von:

> **[Eigener Frontend Controller](https://forum.shopware.com/discussion/10459/eigener-frontend-controller)**
>
> Guten Morgen, kann man über ein Plugin einen Frontend Controller erstellen, den man dann zb. über $args-\\\>getSubject()-\\\>redirect('meinController', 'meineAction'); aufrufen kann?

einen eigenen Frontend Controller erstellt was auch super funktioniert hat - vielen Dank ![Thumb-Up](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/thumb-up.png "Thumb-Up")

Allerdings bin ich jetzt am Ende meines Latein.

Ich habe auf Meiner Controller Seite ein Eingabefeld erstellt und einen Button.

&nbsp;

Bitte geben Sie eine Zahl ein

&nbsp;

So weit so gut. Natürlich werden die Elemente auch gestyled. ![Smile](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/smile.png "Smile")

Jetzt aber meine Frage:

**Wie kann ich die Eingabe an Meinen Controller leiten, dort in einer Action oder Funktion, zu dem eingegebenen Wert die Zahl 10 addieren**

**und unter dem Eingabefeld das Ergebnis ausgeben.**

Kann mir da **BITTE** jemand helfen?

&nbsp;

Vielen Dank im vorraus.

---

<div class="post-metadata">

**Author:** ![hoppler](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@hoppler](https://forum.shopware.com/u/hoppler)\
**Post date:** [24. August 2017 um 07:35 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/2 "2017-08-24T07:35:22Z")

</div>

HTML Code wird leider nicht in der Frage angezeigt - ist aber, denke ich, nicht relevant.

Trotzdem unten ein zweiter Versuch:

""Bitte geben Sie eine Zahl ein

“”

---

<div class="post-metadata">

**Author:** ![hoppler](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@hoppler](https://forum.shopware.com/u/hoppler)\
**Post date:** [25. August 2017 um 20:36 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/3 "2017-08-25T20:36:54Z")

</div>

Hallllllllllooooooooooo,

bin dann doch überrascht dass NIEMAND in der Lage ist so eine Frage zu beantworten!

Wo sind denn die ganzen Entwickler und Programmierer???

Selbst im Buch von Daniel Nörgel „Shopware für Entwickler“ steht das nicht drin!

Echt schade, dass die Hilfsbereitschaft so gering ist.

&nbsp;

---

<div class="post-metadata">

**Author:** ![waldicom](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/waldicom/32/12881_2.png) [@waldicom](https://forum.shopware.com/u/waldicom)\
**Post date:** [26. August 2017 um 09:49 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/4 "2017-08-26T09:49:42Z")

</div>

&nbsp;

```
public function addAction(Enlight_Controller_ActionEventArgs $args)
{
    $request = $this->Request();
    $view = $this->View();
    $myNumber = (int)$request->getParam('myNumber', 0);
    $result = $myNumber + 10;
    $view->assign('result', $result);
}

```

Im Template dann unter dem Eingabefeld die Variable {$result} ausgeben

---

<div class="post-metadata">

**Author:** ![hoppler](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@hoppler](https://forum.shopware.com/u/hoppler)\
**Post date:** [26. August 2017 um 10:58 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/5 "2017-08-26T10:58:48Z")

</div>

Hallo Waldicom,

&nbsp;

erst einmal vielen lieben Dank für Deine Antwort, über die ich mich SEHR gefreut habe.

&nbsp;

Ich habe Deinen Codeschnipsel in den Controller eingefügt, aber leider funktioniert es nicht.

Hier mein gesamter Code:

&nbsp;

Bootstrap.php

\<?php   
 class Shopware\_Plugins\_Frontend\_swagMeincontroller\_Bootstrap extends Shopware\_Components\_Plugin\_Bootstrap { &nbsp;&nbsp;&nbsp; public function getVersion() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return '1.0.0'; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; public function getLabel() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 'Meincontroller'; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; public function getDescription() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 'Meincontroller'; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; public function getInfo() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return array( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'version' =\\> $this-\\>getVersion(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'label' =\\> $this-\\>getLabel(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'description' =\\> $this-\\>getDescription()); &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; public function getCapabilities() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return array( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'install' =\\> true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'enable' =\\> true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'update' =\\> false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; public function install() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $metaDataCache = Shopware()-\\>Models()-\\>getConfiguration()-\\>getMetadataCacheImpl(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $metaDataCache-\\>deleteAll(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-\\>subscribeEvent('Enlight\_Controller\_Dispatcher\_ControllerPath\_Frontend\_Meincontroller', 'onGetControllerPathFrontend'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return array('success' =\\> true, 'invalidateCache' =\\> array('backend', 'proxy')); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; public function enable() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true; &nbsp;&nbsp;&nbsp; } &nbsp; &nbsp;&nbsp;&nbsp; public function uninstall() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp; public function disable() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; public function onGetControllerPathFrontend(Enlight\_Event\_EventArgs $args) &nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $this-\\>Path() . 'Controllers/Frontend/Meincontroller.php'; &nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; &nbsp; } Controllers/Frontend/Meincontroller.php \<?php   
 class Shopware\_Controllers\_Frontend\_Meincontroller extends Enlight\_Controller\_Action { &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function init() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-\\>View()-\\>addTemplateDir(dirname(\_\_FILE\_\_) . "/../../Views/"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function indexAction() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-\\>View()-\\>loadTemplate("frontend/index.tpl"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; } Views/frontend/index.tpl {extends file='frontend/index/index.tpl'} {block name='frontend\_index\_content'} &nbsp;&nbsp;&nbsp; ## Rechenbeispiel &nbsp; Bitte geben Sie eine Zahl ein &nbsp;&nbsp; &nbsp;  
Ergebnis abrufen  
  
 &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;Ergebnis + 10: {$result} {/block} &nbsp; Leider lädt die Seite nun neu und in der Adressleiste steht '/Meincontroller/addAction' - und es wird mir die Startseite angezeigt. Was mache ich falsch? Bin etwas am verzweifeln. Könntest Du bitte noch einmal helfen. &nbsp;

---

<div class="post-metadata">

**Author:** ![EikeBrandtWarneke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@EikeBrandtWarneke](https://forum.shopware.com/u/EikeBrandtWarneke)\
**Post date:** [26. August 2017 um 11:19 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/6 "2017-08-26T11:19:04Z")

</div>

Die URL muss nur /add sein (ohne action). Damit sprichst du die addAction an.

viele Grüße&nbsp;

---

<div class="post-metadata">

**Author:** ![waldicom](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/waldicom/32/12881_2.png) [@waldicom](https://forum.shopware.com/u/waldicom)\
**Post date:** [26. August 2017 um 11:20 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/7 "2017-08-26T11:20:42Z")

</div>

Der Aufruf muss so aussehen: /Meincontroller/add  
Ich sehe aber keine addAction im Controller. Du kannst natürlich die Berechnungen auch in der indexAction() machen. Wenn Du das in der addAction machst, muss Du auch ein passendes Template erstellen. Für die indexAction hast du schon ein Template

&nbsp;

edit: ups, war zu langsam mit der Antwort 🙂

---

<div class="post-metadata">

**Author:** ![hoppler](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@hoppler](https://forum.shopware.com/u/hoppler)\
**Post date:** [26. August 2017 um 11:31 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/8 "2017-08-26T11:31:50Z")

</div>

Habe die URL ausgetauscht, doch wenn ich auf den Button klicke, dann steht in der Adressleiste:

%2FMeincontroller%2Fadd

Leider kann ich keinen HTML Code hier zeigen, da er nach dem auffrischen der Seite verschwindet!!! ![Foot-in-Mouth](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/foot-in-mouth.png "Foot-in-Mouth")

Die addAction habe ich in den Controller gepackt:

&nbsp;public function init()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$this-\>View()-\>addTemplateDir(dirname(\_\_FILE\_\_) . „/…/…/Views/“);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public function indexAction()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $this-\>View()-\>loadTemplate(„frontend/index.tpl“);&nbsp;&nbsp; &nbsp;  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;public function addAction(Enlight\_Controller\_ActionEventArgs $args)  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$request = $this-\>Request();  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$view = $this-\>View();  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$myNumber = (int)$request-\>getParam(‚myNumber‘, 0);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$result = $myNumber + 10;  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$view-\>assign(‚result‘, $result);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}

---

<div class="post-metadata">

**Author:** ![waldicom](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/waldicom/32/12881_2.png) [@waldicom](https://forum.shopware.com/u/waldicom)\
**Post date:** [26. August 2017 um 11:41 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/9 "2017-08-26T11:41:21Z")

</div>

{url controller=/Meincontroller/add} muss so sein:&nbsp;{url controller=Meincontroller action=add}

Und ansonsten kannst Du html Code hier einfügen, wenn du auf diesen Button klickst:&nbsp;

 ![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/3/34219a74fcade6c50b3a0b79e47e9e32a378cd57.png)

---

<div class="post-metadata">

**Author:** ![hoppler](https://avatars.discourse-cdn.com/v4/letter/h/b487fb/32.png) [@hoppler](https://forum.shopware.com/u/hoppler)\
**Post date:** [26. August 2017 um 11:57 UTC](https://forum.shopware.com/t/datenaustausch-zwischen-controller-und-tpl/47786/10 "2017-08-26T11:57:57Z")

</div>

Erst einmal vielen lieben Dank, Waldicom, für Deine Geduld.

O.K, aber so sieht es jetzt aus:

der Controller:

```
View()->addTemplateDir(dirname( __FILE__ ) . "/../../Views/");
        }

        public function indexAction()
        {
		 $this->View()->loadTemplate("frontend/index.tpl");	
		 
		}
		
		public function addAction(Enlight_Controller_ActionEventArgs $args)
		{
			$request = $this->Request();
			$view = $this->View();
			$myNumber = (int)$request->getParam('myNumber', 0);
			$result = $myNumber + 10;
			$view->assign('result', $result);
		}
}

```

in der .tpl:

```
 {extends file="frontend/index/index.tpl"}
{block name="frontend_index_content"}

    {s name='SitemapHeader'} Rechenbeispiel{/s}

Bitte geben Sie eine Zahl ein

	
        Ergebnis abrufen
        
    

Ergebnis: {$result} 
{/block}

```

&nbsp;

komme nun nach dem Buton klicken auf die URL /Meincontroller/add - weiße Seite.

Habe auch versucht das template in der addAction zu laden:

```
$this->View()->loadTemplate("frontend/index.tpl");	

```

kommt leider auch nur eine weiße Seite.
