1

in my symfony2 project i need call the same action in many controllers and this action should return a very simple php array that then will be passed to a twig template by these controllers. How can i do it?

A pratical example can explain my situation better.

1.shared controller

// Acme/DemoBundle/Controller/MetasController

class MetasController extends Controller {

     public function metasAction() { 

         $myArray= array();

         return $myAarray;

      }

}
  1. page render controller

    // Acme/DemoBundle/Controller/PageController
    
    class PageController extends Controller {
    
           protected $property = "test";
    
           public function indexAction() {
    
              $metas= $this->forward('AcmeDemoBundle:Metas:metas');
    
              return $this->render('AcmeDemoBundle:Page:index.html.twig', array('property'=>property,    'metas'=>$metas));
    
           }
    
    } 
    

when i do this i get an error: the controller must be a response array given.

1
  • Don't forward it, since the second function doesn't seem to be an actual Controller but rather just a generic class that returns an array. Instead, make it into a regular class and just call the method. Why is it in a controller in the first place? Commented Sep 23, 2014 at 10:05

4 Answers 4

2

You should create a service

// Acme/DemoBundle/Controller/MetasController
class MetasController {

     public function metasAction() { 

         $myArray= array();

         return $myAarray;

      }

}

declare as service in Acme\DemoBundle\Resources\config\services.yml

services: demo.metas: class: "Acme\DemoBundle\Controller\MetasController"

Then you can use it in any other controller

// Acme/DemoBundle/Controller/PageController

class PageController extends Controller {

       protected $property = "test";

       public function indexAction() {

          $metas= $this->get('demo.metas')->metas();

          return $this->render('AcmeDemoBundle:Page:index.html.twig', array('property'=>property,    'metas'=>$metas));

       }

} 
Sign up to request clarification or add additional context in comments.

Comments

1

In your action controller :

<?php

...

$arrayExample = array();

return $this->render('ExampleBundle:ExampleFolder:exampleTemplate', array('myArray' => $arrayExample));

And in your twig template now you have access to your array using myArray

Example :

{% for data in myArray %}
    ...
{% endfor %}

2 Comments

thanks for the answer but this is not my situation. I have updated my question with some code so maybe can be clearer.
i see but it does not work. i guess the problem its inside the first controller.
0

Try this :

use Symfony\Component\HttpFoundation\Response;

public function indexAction() 
{
      ...

      $content = $this->renderView(
         'AcmeDemoBundle:Page:index.html.twig',
          array('property'=> $property,    
                'metas'   => $metas
          ));

      return new Response($content);
}

Comments

0

Yes, you can register your controller as a service as it said above but I would recommend to isolate this logic in a different place. It might be a service but not controller.

As I understand you need the same array in several places. So, it might be some class registered as service or some simple class with static method providing this array. In this case your code will be much cleaner.

If you need this array only in view you can define custom twig method which will return array you need. If this array might be different time to time (if it might depend on some data) you can pass entity manager to the service providing this array or to the twig extension.

(The best use of controllers is to be just a proxy between view and data layer. It's not a good idea to use it for such purposes as you described (in my opinion of course).)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.