Symfony 2事件监听器和获取URL参数

时间:2015-01-20 14:20:58

标签: php symfony

我有这个事件监听器类:

<?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();
}

}

和这个网址:

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

我想从该网址获取一些参数(id's)。 我怎样才能在事件监听器类中获取它。 当我var_dump this ...

时,变量$ _GET为空

谢谢!

1 个答案:

答案 0 :(得分:3)

您可以将@request_stack注入您的事件监听器,就像您已经使用了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');
}