symfony - Execute function on every route/Action in Symfony3 -
i have logging function tracks pageloads. add every route have in symfony.
currently doing this:
/** * @route("/test", name="test") */ public function testaction(request $request) { /* function register pageload */ $this->get('statsd')->countmetric('visitor.'.$request->get('_route')); return $this->render('default/indexfull.html.twig', array()); }
i of course add line every route, since new symfony thought there might more elegant approach?
any hint appreciated!
symfony triggers events during handling of request. 1 of these events kernel.request
event, executed before each controller.
be aware router matching during event well, need make sure listener executed after router listener. can done using priorities.
use symfony\component\httpkernel\event\getresponseevent; class countmetriclogger { // ... public function logvisit(getresponseevent $event) { $this->statsd->countmetric('visitor.'.$event->getrequest()->get('_route')); } }
# app/config/services.yml app.metric_logger: class: appbundle\eventlistener\countmetriclogger arguments: ['@statsd'] tags: - { name: kernel.event_listener, event: kernel.request, method: logvisit }
more information:
Comments
Post a Comment