简体   繁体   中英

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!

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM