Reputation: 9183
Version: Symfony 3
Issue: Accessing Resource from an event listener's method, such as "onKernelBefore".
I have the following method which gets called right before invocation of a controller:
function onKernelController(\Symfony\Component\HttpKernel\Event\FilterControllerEvent $controllerEvent)
{
$controller = $controllerEvent->getController();
/*
* $controller passed can be either a class or a Closure.
* This is not usual in Symfony but it may happen.
* If it is a class, it comes in array format
*/
if (!is_array($controller)) {
return;
}
$Helper = new Helper();
$current_path = $Helper->cleanSlashes($controllerEvent->getRequest()->getPathInfo());
if ($controller[0] instanceof RouteAuthenticateInterface) {
print "Passed";
}
}
But I do not now how to access certain resources such as Doctrine or Request etc. I event have accessed my helper through direct instanciaion:
$Helper = new Helper();
While it is common to access it from the controller like this:
$this->get("Helper");
For instance I can't access this:
$this->container
I know the pseudo-variable $this
is referring to a resource other than a controller; but even when I use $controller[0]->container
I bump into "protected property exception".
What should I do? Thanks
Upvotes: 0
Views: 338
Reputation: 242
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
...
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
....
Or a better example coming from the FOSUserBundle:
...
public function __construct(MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator,
UrlGeneratorInterface $router, SessionInterface $session, Container $container)
{
$this->mailer = $mailer;
$this->tokenGenerator = $tokenGenerator;
$this->router = $router;
$this->session = $session;
$this->container = $container;
}
...
Upvotes: 1