Execute a controller's function on every page load - php

Is there a way on Symfony2 to call a controller function on each page load? At the momment my solution is using an ajax call, but i'll like to solve this all in the backend part. (ofcourse without having to copy the function name on each controller function)

You can create Event Listener and handle KernelEvents::CONTROLLER event with it (before filter), as described here.
Example:
Acme\DemoBundle\EventListener\DemoListener.php
namespace Acme\DemoBundle\EventListener;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class DemoListener
{
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
/*
* $controller passed can be either a class or a Closure.
* This is not usual in Symfony2 but it may happen.
* If it is a class, it comes in array format
*/
if (!is_array($controller)) {
return;
}
$controller[0]->fooBarMethod();
}
}
Acme\DemoBundle\Resources\services.yml
parameters:
acme_demo.event_listener.class: Acme\DemoBundle\EventListener\DemoListener
services:
acme_demo.event_listener:
class: %acme_demo.event_listener.class%
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }

Related

How to create exception event listener for a specific controller action, to redirect to another action using its original argument?

I have an action which takes an argument, the route looks like this:
/cs/{id}
It's the individualAction in the Cassette controller.
Sometimes, there's an exception 500 Internal Server Error - NoResultException - this is expected behaviour.
I am looking to redirect to another controller action, editAction, when this happens, the route is as such:
/cs/{id}/edit
It needs to be controller-specific, since I want to repeat this with different controller actions. It also needs to keep the argument from the original action.
I've been looking into event listeners, but I'm not sure if that's overkill and I'm struggling to find out how to make them action-specific - I'm happy to keep the code in the controller if that's the better solution.
An EventListener is the most appropriated to change the comportment of your app when an exception is thrown.
Use something like :
<?php
namespace AppBundle\EventListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Routing\RouterInterface;
use Doctrine\ORM\NoResultException;
class ExceptionResponseListener
{
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
/**
* #param GetResponseForExceptionEvent $event
*/
public function onKernelResponse(GetResponseForExceptionEvent $event)
{
$request = $event->getRequest();
$routeName = $request->get('_route');
$exception = $event->getException();
// Restrict the route by adding a check on the route name
// Or a pattern of route names (!strpos($routeName, '_show'))
// Or for many routes: (!in_array($routeName, ['xxx_show', ...]
// Or the _controller: $request->get('_controller')
// Output something like: "AcmeBundle\\Controller\\DefaultController::showAction"
if ('expected_route_name' !== $routeName) {
return;
}
// Retrieve your param
$params = $request->get('_route_params');
if ($exception instanceof NoResultException) {
// Create a redirection to the edit route
$response = new RedirectResponse(
$this->router->generate('your_edit_route', array('id' => $params['id']))
);
$event->setResponse($response);
}
}
}
And register it as service :
services:
# ...
acme.kernel.listener.exception_listener:
class: AppBundle\EventListener\ExceptionResponseListener
tags:
- {name: kernel.event_listener, event: kernel.exception, method: onKernelResponse}
arguments: ['#router']
Another simple alternative could be to make a forward/redirection of the expected action directly in your method, instead of throw the exception.

Cannot get parameters to controller using event listener in Symfony2

I am currently using the Symfony2 event listener to change the controller to a different one based on a users authentication status. I get the listener to set the new controller but it is instantiated without the container parameter (i.e. $this->container returns null).
Is there anyway to pass the container on to the controller I am changing to?
class AuthenticationListener
{
public function onController(FilterControllerEvent $event)
{
$request = $event->getRequest();
$session = $request->getSession();
if (!$session->has('authenticated') || $session->get('authenticated') === false)
{
$controller = $event->getController();
if (!($controller[0] instanceof AuthenticateController) && !($controller[0] instanceof ExceptionController))
{
$event->setController(array(new AuthenticateController(), 'loginAction'));
}
}
}
}
The container is not set, when you create the controller automatically. Call setContainer after constructing the controller. Afterwards you can pass it to the event.
In this case AuthenticationListener its just a class
if you want to use $this->container in this class you must do like this:
class BeforeControllerListener extends ContainerAware
{
...
}
and in config.yml
core.listener.before_controller:
class: App\YourBundle\EventListener\YourListener
tags: [ {name: kernel.event_listener, event: kernel.controller, method: onKernelController}]
calls:
- [ setContainer,[ #service_container ]]

How to inject controller into EventDispatcher in Symfony2

I want to do some post-processing after sending a response object in my Symfony controller. Problem is, the post-processing requires other methods contained in my controller object. I'd like to do something like this:
public function testAction() {
$dispatcher = new EventDispatcher();
$dispatcher->addListener('kernel.terminate', function (Event $event) {
$controller->get('logger');
$logger->info('hello');
});
return new Response();
}
How can I inject the $controller variable into my kernel.terminate post-processing?
it seems you need only the container in your service. To get the container injected into your event listener I prefer to create a separate EventListener which you have to register in your container see code:
First create event listener class:
<?php
namespace Acme\DemoBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RequestListener
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$logger = $this->container->get('logger')->getToken();
$logger->info('.....');
}
}
As you can see, we have now the service container injected and we are able to use it.
Next you have to register the service and inject the service container:
services:
acme.demo.listener.request:
class: Acme\DemoBundle\Listener\RequestListener
arguments: [ #service_container ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Notice in your case you have to select the event you wanna inject to. In my case I used the kernel.request event. You have to select the kernel.terminate event.
That can also be helpful: http://symfony.com/doc/current/cookbook/service_container/event_listener.html

Why is symfony2 not calling my event listeners?

I have a program with two bundles. One of them (CommonBundle) dispatches an event "common.add_channel", while a service on the other one (FetcherBundle) was supposed to be listening to it. On the profiler, I can see the event common.add_channel in the "Not Called Listeners" section. I don't get why symfony is not registering my listener.
This is my action, inside CommonBundle\Controller\ChannelController::createAction:
$dispatcher = new EventDispatcher();
$event = new AddChannelEvent($entity);
$dispatcher->dispatch("common.add_channel", $event);
This is my AddChannelEvent:
<?php
namespace Naroga\Reader\CommonBundle\Event;
use Symfony\Component\EventDispatcher\Event;
use Naroga\Reader\CommonBundle\Entity\Channel;
class AddChannelEvent extends Event {
protected $_channel;
public function __construct(Channel $channel) {
$this->_channel = $channel;
}
public function getChannel() {
return $this->_channel;
}
}
This was supposed to be my listener (FetcherService.php):
<?php
namespace Naroga\Reader\FetcherBundle\Service;
class FetcherService {
public function onAddChannel(AddChannelEvent $event) {
die("It's here!");
}
}
And here's where I register my listener (services.yml):
kernel.listener.add_channel:
class: Naroga\Reader\FetcherBundle\Service\FetcherService
tags:
- { name: kernel.event_listener, event: common.add_channel, method: onAddChannel }
What am I doing wrong? Why isn't symfony calling the event listener when I dispatch common.add_channel?
The new event dispatcher doesn't know anything about the listeners set on another dispatcher.
In your controller, you need to access the event_dispatcher service. A Compiler Pass of the Framework Bundle attached all listeners to this dispatcher. To get the service, use the Controller#get() shortcut:
// ...
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ChannelController extends Controller
{
public function createAction()
{
$dispatcher = $this->get('event_dispatcher');
// ...
}
}

Symfony 2.1 - Custom Json Annotation Listener

For a Symfony 2.1 project, I'm trying to create a new annotation #Json() that will register a listener that will create the JsonResponse object automatically when I return an array. I've got it working, but for some reason the listener is always called, even on methods that don't have the #Json annotation. I'm assuming my approach works, since the Sensio extra bundle does this with the #Template annotation.
Here is my annotation code.
<?php
namespace Company\Bundle\Annotations;
/**
* #Annotation
*/
class Json extends \Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationAnnotation
{
public function getAliasName()
{
return 'json';
}
}
Here is my listener code.
<?php
namespace Company\Bundle\Listener\Response\Json;
class JsonListener
{
//..
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$data = $event->getControllerResult();
if(is_array($data) || is_object($data)) {
if ($request->attributes->get('_json')) {
$event->setResponse(new JsonResponse($data));
}
}
}
}
This is my yaml definition for the listener.
json.listener:
class: Company\Bundle\Listener\Response\Json
arguments: [#service_container]
tags:
- { name: kernel.event_listener, event: kernel.view, method: onKernelView }
I'm obviously missing something here because its being registered as a kernel.view listener. How do I change this so that it is only called when a #Json() annotation is present on the controller action?
Not pretend to be the definitive answer.
I'm not sure why your are extending ConfigurationAnnotation: its constructor accepts an array, but you don't need any configuration for your annotation. Instead, implement ConfigurationInterface:
namespace Company\Bundle\Annotations;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
/**
* #Annotation
*/
class Json implements ConfigurationInterface
{
public function getAliasName()
{
return 'json';
}
public function allowArray()
{
return false;
}
}
Sensio ControllerListener from SensionFrameworkExtraBundle will read your annotation (merging class with methods annotations) and perform this check:
if ($configuration instanceof ConfigurationInterface) {
if ($configuration->allowArray()) {
$configurations['_'.$configuration->getAliasName()][] = $configuration;
} else {
$configurations['_'.$configuration->getAliasName()] = $configuration;
}
}
Setting a request attribute prefixed with _. You are correctly checking for _json, so it should work. Try dumping $request->attributes in your view event listener. Be sure that your json.listener service is correctly loaded too (dump them with php app/console container:debug >> container.txt).
If it doesn't work, try adding some debug and print statements here (find ControllerListener.php in your vendor folder):
var_dump(array_keys($configurations)); // Should contain _json
Remember to make a copy of it before edits, otherwise Composer will throw and error when updating dependencies.

Categories