Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 Event Listener And Get URL Parameters

Tags:

php

symfony

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!

like image 555
Thomas Crawford Avatar asked Dec 29 '25 21:12

Thomas Crawford


1 Answers

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');
}
like image 191
Markus Kottländer Avatar answered Jan 01 '26 11:01

Markus Kottländer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!