0

I'm using a repository in Symfony2-controller like this:

$blog = $em->getRepository('BlogBloggerBundle:BlogData')->getBlogData($id);

Before I call it into the twig view through an array.

All works right but the issue is that it make up a footer menu, then, i should call it almost in every action which i need.

How i can call it from a "common" repository every time which i need?

Roberto Rizzi
  • 1,525
  • 5
  • 26
  • 39

2 Answers2

3

Create a view reponse listener registering a twig variable and add the repository call in there...

... or create a twig extension exposing the data received from the repository as a global twig variable.

... or (my preferred choice) create a controller dedicated to rendering the footer and include the footer like this:

{{ render(controller('Bundle:controller:action')) }}

Read more about rendering fragments in this blog post on the symfony homepage.

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • good idea. I'm loading data from a repository: `$brand = $em->getRepository('BloggerBlogBundle:BlogData')->getBlogData($id); return $brand);` And i get this error: `an exception has been thrown during the rendering of a template ("The controller must return a response (Array(0 => Array(0 => Array .... ` – Roberto Rizzi Oct 04 '13 at 15:55
  • `);` seems to be a syntax error shouldn't it be `return $brand;` ? – Nicolai Fröhlich Oct 04 '13 at 16:21
  • 1
    As you're trying to return the entity directly and not a `Response` object ... did you add the `@View` annotation to your controller ... or do you have some other controller response listener (i.e. FOSUserBundle's) in place ? ... Otherwise your need to do something like this: `return $this->render( 'BloggerBlogBundle:Footer:index.html.twig', array('brand' => $brand) );` – Nicolai Fröhlich Oct 04 '13 at 16:35
  • Thanks a lot [nifr](http://stackoverflow.com/users/1832745/nifr)!!! Finally i solved it! I lost the `$this->render( 'Blogger...` part. With yours suggestions it works perfectly! – Roberto Rizzi Oct 07 '13 at 15:24
0

FYI Official documentation about embedding controllers : http://symfony.com/doc/current/book/templating.html#embedding-controllers

zapcost
  • 1,336
  • 12
  • 5