Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

twig - Symfony 2.0.3 Global template variable

I've got an Entity that I want to associate with the users session. I created a service so that I could reach this info from where ever.

in the service i save the entities id in an session variable and in the getEntity() method i get the session variable and with doctrine find the entity and return it.

this way to the template i should be able to call {{ myservice.myentity.myproperty }}

The problem is that myservice is used all over the place, and I don't want to have to get it in every since Action and append it to the view array.

Is there a way to make a service accessible from all views like the session {{ app.session }} ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution

By creating a custom service i can get to that from where ever by using

$this->get('myservice');

this is all done by http://symfony.com/doc/current/book/service_container.html

But I'll give you some demo code.

The Service

This first snippet is the actual service

<?php
namespace MyBundleAppBundleExtensions;

use SymfonyComponentHttpFoundationSession;
use DoctrineORMEntityManager;
use MyBundleAppBundleEntityPatient;

class AppState
{
    protected $session;
    protected $em;

    function __construct(Session $session, EntityManager $em)
    {
        $this->session = $session;
        $this->em = $em;
    }

    public function getPatient()
    {
        $id = $this->session->get('patient');
        return isset($id) ? $em->getRepository('MyBundleStoreBundle:Patient')->find($id) : null;
    }
}

Register it in you config.yml with something like this

services:
    appstate:
        class: MyBundleAppBundleExtensionsAppState
        arguments: [@session, @doctrine.orm.entity_manager]

Now we can as I said before, get the service in our controllers with

$this->get('myservice');

But since this is a global service I didn't want to have to do this in every controller and every action

public function myAction()
{
    $appstate = $this->get('appstate');
    return array(
        'appstate' => $appstate
    );
}

so now we go create a Twig_Extension

Twig Extension

<?php
namespace MyBundleAppBundleExtensions;

use MyBundleAppBundleExtensionsAppState;

class AppStateExtension extends Twig_Extension
{
    protected $appState;

    function __construct(AppState $appState) {
        $this->appState = $appState;
    }

    public function getGlobals() {
        return array(
            'appstate' => $this->appState
        );
    }

    public function getName()
    {
        return 'appstate';
    }

}

By using dependency injection we now have the AppState Service that we created in the twig extension named appstate

Now we register that with the symfony (again inside the services section inside the config-file)

twig.extension.appstate:
    class: MyBundleAppBundleExtensionsAppStateExtension
    arguments: [@appstate]
    tags:
        - { name: twig.extension }

The important part being the "tags", since this is what symfony uses to find all twig extensions

We are now set to use our appstate in our twig templates by the variable name

{{ appstate.patient }}

or

{{ appstate.getPatient() }}

Awesome!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...