3

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!

4
  • Questions on StackOverflow have to be relevant and helpful to more people than just the poster. Commented Jan 20, 2015 at 14:29
  • @franssu "Questions on StackOverflow have to be relevant and helpful to more people than just the poster" - And how would you suggest we define or moderate such a thing in a sane and fair way. And what is "more"? Who decides this "more"? Is it 3? 10 people? Besides, 3K views would make me assume it was helpful to some people, let's say 30 people at a reasonable and arguably generous 1%. Commented Jul 16, 2020 at 15:08
  • I agree with @James, i asked this question a long time ago and i'm sure the answers helped some people, it helped me 5 years ago! Commented Jul 22, 2020 at 13:33
  • 1
    upvoted the question. Commented Oct 24, 2020 at 14:35

1 Answer 1

4

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');
}
Sign up to request clarification or add additional context in comments.

3 Comments

I don't believe that this is correct or at least that simple. In order to get access to the query parameters you need access to the Request object, That part is correct. But I beleive that you gain access to this object via the service container and it takes a little more configuration than you have explained there. See this other answer. Could you elaborate on where the requestStack variable is filled from
I slightly adjusted my answer. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.