0

I have a Symfony event listener which is listening on "kernel.request". I would like to add an attribute to the request which I can access in my controllers. I tried doing this:

$request = $event->getRequest();
$request->attributes->set('test', 'testvalue');

However, the "test" attribute is not a part of the request object in my controllers. I am not receiving any errors, but it's just not there. What else do I need to do?

edit:

I verified that the listener is being called. In my controller, I have this, but don't get the "test" attribute:

class DefaultController extends BaseController
{
    public function indexAction(Request $request)
    {
        echo "<pre>"; print_r($request->attributes); echo "</pre>";
    }
}
4
  • 1
    What you posted should work. Verify your listener is actually being called. And that the controller is using get or the action has an argument called $test. Commented Jun 4, 2014 at 21:51
  • Which Symfony version? 2.4+? Commented Jun 5, 2014 at 0:12
  • 1
    Cerad: what I think that Steven is trying to do is to inject arbitrary arguments into the response. Commented Jun 5, 2014 at 1:59
  • just updated my question with more details Commented Jun 5, 2014 at 12:54

1 Answer 1

1

It turns out my Event Listener was returning before my attribute was being set. Stupid mistake.

Sign up to request clarification or add additional context in comments.

1 Comment

what is the solution?

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.