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.

4
  • I assume that you tried $this->arrays = ...., right? Oct 21, 2014 at 8:29
  • No i did not tried like this.i am unable to find any solution for this i searched everything google. Oct 21, 2014 at 9:00
  • When you write $array = you set the local variable, which is not visible outside of that method. You must use $this->arrays in order to reference class member... Oct 21, 2014 at 9:07
  • Can you please have a look into this.i m doing like following. Oct 21, 2014 at 9:11

2 Answers 2

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.

1
  • I agree with this, completely. Oct 21, 2014 at 10:46
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']

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.