0

Hi i have following function in controller Symfony2

class OrganisationTaskController extends Controller
{

    public function setContainer(\Symfony\Component\DependencyInjection  \ContainerInterface $container = null)
    {
        parent::setContainer($container);
        $this->containerInitialized();
    }

    /**
     *    Perform some operations after controller initialized and container set.
     */
    public function containerInitialized()
    {

        $dbName = $_SESSION['_sf2_attributes']['dbName'];
        $orgDM = $this->get('work_core.common_functions')->getDocumentManager('org', $dbName);
        $coreDM = $this->get('work_core.common_functions')->getDocumentManager('core', '');

        $organisationLogoDetails = $orgDM->getRepository(
            'Work\OrganisationBundle  \Document\OrganisationConfig'
        )->findOneBy(
            array(
                'isActive' => true
            )
        );
        if ($organisationLogoDetails->getlogoFileId() != "") {
            $orgLogo = $organisationLogoDetails->getlogoFileId();
        } else {
            $orgLogo = "";
        }
        //Get Organisation Name from Global Organisation
        $globalOrgName = $coreDM->getRepository('Work\CoreBundle\Document\Organisation')->findOneBy(
            array('shortName' => $_SESSION['_sf2_attributes']['shortName'])
        );
        $orgName = $globalOrgName->getName();


        $commonVariablesArrays = array(
            'orgLogo' => $orgLogo,
            'orgName' => $orgName
        );

    }
}

I want to pass $commonVariablesArrays variable in layout.html.twig.Without rendering.

this function called before any function of controller where i am writing some common code which i need to use in my layout.html.twig file which is common layout for my application. So my problem is i am unable to set $arrays variable to pass this "layout.html.twig". and i am extending this layout on each page like: {% extends 'WorkCoreBundle::layout.html.twig' %}

So can anyone plese help me how to access this $array variable in layout file.Great full to you.If i am going to pass this variable like: return $this->render('WorkOrganisationBundle:Layout:layout.html.twig', array('aray'=>$commonVariablesArrays ));

This also throwing error that variable aray does not exist.

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
Sunil Rawat
  • 709
  • 10
  • 41

2 Answers2

2

If containerInitialized is kind of __construct in your Controller, then you need to set $this->commonVariablesArrays there and then you can retrieve this common array into the controller::Actions()

Anyways, I would take a different route and do this common assignements elsewhere, for example in a service. Then you can inject the service in the controllers where you need it, or inject it for all the controller::Actions() as a property using http://jmsyst.com/bundles/JMSDiExtraBundle (Although is just a recommendation, you can achieve this using standard Symfony without any extra-bundles)

Another way to achieve what you are looking for is using Event Handlers. Take a look here: http://symfony.com/doc/current/components/http_kernel/introduction.html

I don't understand the concept of passing values into a Template without rendering. Is precisely when you render, that the key->values are assigned in the template.

Martin Fasani
  • 825
  • 7
  • 20
0

I'd suggest using session as it is stated in following response:

Accessing session from TWIG template

and never access session global manually $_SESSION['_sf2_attributes']

Community
  • 1
  • 1
Bartłomiej Wach
  • 1,968
  • 1
  • 11
  • 17