symfony2 - What is the recommended method for setting global variables that are accessible in Listeners and Controllers? -
background
i'm building hosted cms symfony 2.5, , in 1 scenario observe right site business relationship based on domain. when user creates records, utilize prepersist
automatically insert siteid
database record (so records associated right account). getting siteid
injected listener loaded service, complicated.
i can't set siteid
in config parameters (because it's dynamic each site, not set value whole app).
and can't utilize $container->setparameter('siteid', $siteid)
because parameters can't modified time controller executes.
and in fact there lot of environmental variables need access across project, can't manually injected time. soi know others recommend solution, , whether solution "okay".
current solutioni need store lot of cms environmental variables accessible in various controllers, services , listeners. i've created env
entity, can store these values later access, , defined service can injected listeners.
environment entity
namespace gutensite\cmsbundle\entity\cms; class env { private $siteid = 0; public function getsiteid() { homecoming $this->siteid; } public function setsiteid($siteid) { $this->siteid = $siteid; homecoming $this; } }
define service.yml
services: #define env service gutensite_cms.env: class: gutensite\cmsbundle\entity\cms\env #inject env listener gutensite_cms.listener.entity_persist: class: gutensite\cmsbundle\eventlistener\entitypersistlistener arguments: - "@gutensite_cms.env" tags: - { name: doctrine.event_listener, event: prepersist } - { name: doctrine.event_listener, event: preupdate }
event listener add together siteid
namespace gutensite\cmsbundle\eventlistener; utilize doctrine\orm\event\lifecycleeventargs; utilize doctrine\orm\event\onflusheventargs; utilize gutensite\cmsbundle\entity\cms\env; class entitypersistlistener { private $env; public function __construct(env $env) { $this->env = $env; } public function prepersist(lifecycleeventargs $eventargs) { $entity = $eventargs->getentity(); if (method_exists($entity, 'settime')) $entity->settime(time()); if (method_exists($entity, 'setsiteid') && !$entity->getsiteid()) $entity->setsiteid($this->env->getsiteid()); } public function preupdate(lifecycleeventargs $eventargs) { $entity = $eventargs->getentity(); if (method_exists($entity, 'settimemod')) $entity->settimemod(time()); // update parent view timemod $this->updateview($eventargs); } public function updateview($eventargs) { $em = $eventargs->getentitymanager(); $uow = $em->getunitofwork(); $updatedentities = $uow->getscheduledentityupdates(); foreach($updatedentities $entity) { $view = false; if (method_exists($entity, 'getviewversion')) { $view = $entity->getviewversion()->getview(); } if (method_exists($entity, 'getview')) { $view = $entity->getview(); } if($view) { $view->settimemod(time()); } } } }
the big question i not want introduce bad solution model break dependency injection. store environmental variables need access in lots of different controllers , listeners?
in app, content separated between "organisations". user can belong many of , can switch between organisations after logging in. entities created user belong current organisation. create sure don't have set explicitly each time add together new entity, created listener:
class boundtoorganisationlistener implements eventsubscriber { (...) public function getsubscribedevents() { homecoming array( events::prepersist ); } public function prepersist(lifecycleeventargs $args) { $entity = $args->getentity(); $em = $args->getentitymanager(); /* @var $em \doctrine\orm\entitymanager */ $classmetadata = $em->getclassmetadata(get_class($entity)); if (array_key_exists('organisation', $classmetadata->reflfields) && !$entity->getorganisation()) { $token = $this->securitycontext->gettoken(); if (empty($token)) { } else if($organisationid = $this->getcurrentuserorganisationid()) { $organisation = $em->find('...:organisation', $organisationid); $entity->setorganisation($organisation); } }
i maintain current organisation in user object(i save database well) can current organisation user in security.context service .
i check !$entity->getorganisation() in case want add together different organisation 1 user in(for illustration when i'm admin or something) setting manually new entity.
my advise maintain siteid in session , inject session service makes sure new objects have proper siteid set.
to save siteid session, can utilize same mechanism sensio advises setting locale
http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
you can siteid based on request sure.
symfony2 dependency-injection listeners
No comments:
Post a Comment