1

I am trying to access my slug parameters outside my controller.

I have a Routing YML like this:

adgroup:
    pattern:  /adgroup/{id}
    defaults: { _controller: ExampleBundle:AdGroup:index }
    requirements:
        id: \d+

..and a URL like this:

http://example.com/adgroup/25

I need a way to access the {id} variable without getting it from my controller, my controller is working perfectly. However, I am trying to build an Object that will rely heavily based on which Slugs are being passed in.

So far scouring the docs and many, many examples has left me no where.

What Ive tried: I've var_dumped the entire Request::createFromGlobals method, and it's children to see if its stored in there in anyway. It does not appear so.

Also, since I am on a development environment, the debug toolbar in symfony clearly shows Request: id: 25 in the profiler.

So the question is... How do I get my slugs / slug values from outside the controller?

I have way too many controllers to attempt to pass them in from there 1 by 1, and hacking it from exploding the URL is just a bad idea. :-)

I imagine there is some method I am unaware of to access these?

2
  • 1
    What do you mean by "outside the controller"? In a view? In a service? In your garden? Commented Nov 16, 2012 at 18:03
  • @moonwave99, sorry! I'm working inside a Model. Didn't think it was important since I can namespace/use any classes I would need. This model is a custom service. Commented Nov 16, 2012 at 18:04

1 Answer 1

8

I think this code will help you

$params = $container->get('request')->attributes->get('_route_params');
$id = $params['id'];
Sign up to request clarification or add additional context in comments.

3 Comments

What do you use to get $container? $container = MagicalClass::getsMeWhatINeed(); type thing?
if "This model is a custom service" so i think u can pass container to its constructor using DIC
Actually cool! Symfony documentation and api doesn't really focus help on how to handle this.

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.