Alex Rivera | Logout

Lookup route in symfony 2

Asked 2012-09-11T14:23:59.620
13

I've defined a route in my app routing file:

RouteName:
    pattern:  /some/route
    defaults: { _controller: MyAppBundle:Controller:action }

In a controller I can use:

$this->get('router')->generate('RouteName');

How would I simply access that from a fresh class I create, for example a view class that doesn't extend anything:

namespace My\AppBundle\View;

class ViewClass {
    public function uri()
    {
        return getTheRoute('RouteName');
    }
}
Edit
Report

1 Answer

1

You should register your class as a service and insert the router as a dependency.

See the chapter on the service container in the excellent symfony2 docs.

If you're not familiar with the concepts of the service container and dependency injection, you might feel a bit overwhelmed. However, try your best to understand it because it is a essential part of the symfony2 architecture.

answered 2012-09-11T14:29:29.080

Your Answer