php - Symfony 2.2 - no request scope in Voter -
i'm trying migrate symfony 2.1 version 2.2.1. use own voter deciding if user granted access given route. voter simple , worked before update. problem voter needs request service parameters required check if user can access site (it id given in route, e.g. /profile/show/{userid}). check if request scope active prevent error when using cli or phpunit:
$this->request = null; if ($container->isscopeactive('request')) { $this->request = $container->get('request'); }
and later throw exception if there no request in vote method:
if ($this->request === null) { throw new \runtimeexception("there's no request in profilevoter"); }
i got exception after every vote calling (= on every page of app).
edit: happens in dev environment.
according symfony2.2 documentation:
"take care not store request in property of object future call of service cause same issue described in first section (except symfony cannot detect wrong)." ( http://symfony.com/doc/current/cookbook/service_container/scopes.html#using-a-service-from-a-narrower-scope )
in solution, check container scope activity in constructor, , if have active scope, store in $this->request. however, correct approach store not request, container itself:
protected $container; public function __construct(containerinterface $container) { $this->container = $container; }
an later, in method (as see, not in constructor), check scope activity:
public function vote(...) { if ($this->container->isscopeactive('request')) { $request = $this->container->get('request'); } else { throw new \runtimeexception("there's no request in profilevoter"); } }
Comments
Post a Comment