php - How to make object available throughout symfony2 app without repeating the query? -


how can share object across views in symfony?

i implemented class serializable in entity, store object in sessions. however, since entity contains abstract methods need implement serializable method in class, believe last resort.

until then, i'd find alternative solution other storing object in temporary table in database (it same not doing because need call database in every controller displays view)

this scenario:

i have 2 files: layout.html.twig , sidebar.html.twig

i have method inside defaultcontroller called buildsidebar() looks follows

public function buildsidebar($userid = 0) {     $projects = $this->getdoctrine()->getrepository('superbundle:projects')->findby(array('userid' => $userid), array('crateddate' => desc), 10);     return $this->render(             'superbundle:sidebar.html.twig',             array('projects' => $projects)     ); } 

then in layout.html.twig file have following block of code in line 77:

{% render controller("superbundle:default:buildsidebar", {'userid': 37})  %} 

and when test out, symfony throws exception saying:

function controller doesn't exist in superbundle::layout.html.twig in line 77

i've tried embed controller way:

{{ render(controller("superbundle:default:buildsidebar", {'userid': 37}) }} 

this method throws exception unexpected name of value controller:

{{ render controller("superbundle:default:buildsidebar", {'userid': 37}) }} 

i tried way, , same result function controller not exist:

{% render(controller("superbundle:default:buildsidebar", {'userid': 37}) %}

what missing? followed symfony's documentation letter, , nothing in there has helped me either.

yes can access service twig extention. http://symfony.com/doc/current/cookbook/templating/twig_extension.html

<?php  namespace you\userbundle\twig;  use symfony\component\security\core\securitycontext;   class yourextension extends \twig_extension {      protected $doctrine;      function __construct($doctrine)     {         $this->doctrine = $doctrine;     }       public function getusers()     {         $em = $this->doctrine->getmanager();         $result = $em             ->getrepository('yourentity')->findall();         return $result;     }      public function getfunctions()     {         return array(             new \twig_simplefunction('get_users', array($this, 'getusers'))         );     }      public function getname()     {         return 'your_extension';     }  } 

or have possibility include 1 controller in twig view :

        {{ render(controller('youruserbundle:yourcontroller:youraction')) }} 

source : http://symfony.com/blog/new-in-symfony-2-2-the-new-fragment-sub-framework


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -