Thomas Crawford
Thomas Crawford

Reputation: 896

Symfony 2 Event Listener And Get URL Parameters

I have this event listener class :

<?php

namespace Vdv\TimesheetsBundle\Event;

use Oneup\UploaderBundle\Event\PostPersistEvent;
use Symfony\Component\HttpFoundation\Request;

class UploadListener {

public function __construct($doctrine) {
    $this->doctrine = $doctrine;
}

public function onUpload(PostPersistEvent $event) {
    $request = $event->getRequest();
}

}

and this url :

http://localhost/vdvinfra/web/app_dev.php/timesheet/add/1/252

i want to get some parameters(id's) from that url. How can i get it in a event listener class. The variable $_GET is empty when i var_dump this...

Thanks!

Upvotes: 3

Views: 4526

Answers (1)

Markus Kottl&#228;nder
Markus Kottl&#228;nder

Reputation: 8268

You can inject the @request_stack into your event listener like you already did with doctrine:

public function __construct($doctrine, $requestStack) {
    $this->doctrine = $doctrine;
    $this->request = $requestStack->getCurrentRequest();

    // access the parameters like this:
    $allParams = $this->request->attributes->all();
    $someParam = $this->request->attributes->get('parameter_name');
}

Upvotes: 4

Related Questions