I have a form builder class which inherits from AbstractType and I need to resolve a path like this:
$uri = $router->generate('blog_show', array('slug' => 'my-blog-post'));
Since the class is not a child of Controller I have no access to the router. Any ideas?
What about passing the router to the class on construction time?
You can pass the
routerservice via constructor to your form type. Register your form as a service with theform.typetag and inject therouterservice to it.Register it as a service:
And use it in your controller like this:
Since you registered your form type as a service with the
form.typetag, you can simply use its name instead ofnew PostType(). And you can access therouterservice as$this->routerin your type.