14

Is there a possibility to use generateUrl() method outside of controllers?

I tried to use it in a custom repository class with $this->get('router'), but it didn't work.

update

I've found a temporary solution here:

http://www.phamviet.net/2012/12/09/symfony-2-inject-service-as-dependency-in-to-repository/

I injected the whole service container into my repository, although it's "not recommended".

But it works for now.

update2

Injecting router instead of the whole container is probably a better idea :)

Sergejs Rižovs
  • 458
  • 1
  • 4
  • 11

4 Answers4

17

If you take a look in the source code of Controller::generateUrl(), you see how it's done:

$this->container->get('router')->generate($route, $parameters, $referenceType);

Basically you just enter the name of the route ($route here); if exists, some parameters ($parameters) and the type of reference (one of the constants of the UrlGeneratorInterface)

Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • 1
    But $this->container is also not available in repositories. How do i pass container to them? – Sergejs Rižovs Mar 17 '13 at 08:05
  • You completely missed the point of the question in this answer!! Use of ""this" assumes that you're inside the controller. – cartbeforehorse Aug 23 '13 at 15:24
  • 2
    @cartbeforehorse no, this asumes assumes your in an object which can be instantiated... – Wouter J Aug 23 '13 at 19:45
  • 2
    @WouterJ I think you missed the point of my comment. The whole point of the question is that `$this->container` and `$this->generateUrl()` cannot be used, because the class we're coding in is not an extension of the `Controller` (or `ContainerAware`) object. Unless I missed something. – cartbeforehorse Aug 25 '13 at 02:01
11

Don't inject the container into your repository... Really, don't !

If I were you, I would create a service and injects the router in it. In this service, I would create a method, that uses the repository and adds the needed code using the router.

That's way less dirty and easy to use/understand for another developer.

Gmajoulet
  • 700
  • 4
  • 8
  • injecting just router is also a bad idea? – Sergejs Rižovs Mar 17 '13 at 16:12
  • 2
    It's a better idea but unfortunately still not a good idea. :< Repositories purpose is to write some custom queries, no doing stuff like redirection. It's actually more a matter of good practices. – Gmajoulet Mar 17 '13 at 17:34
  • Gmajoulet, perhaps some links to the documentation would help – Thomas Potaire Mar 18 '13 at 05:24
  • http://symfony.com/doc/master/book/service_container.html Like I said : try to create a service, and inject the router component in it (and the entitymanager to get your repository). Then you'll be able to work with the router and with your repository logic, in a clean way. – Gmajoulet Mar 18 '13 at 13:17
  • the way that you cannot define what kind of interface is returned makes it actually not really easier to understand for a developer – Toskan May 26 '14 at 01:41
  • I think that this answer should be chosen as the right one. – Jesús Flores Aug 04 '14 at 13:07
2

Inject the router itself into your EntityRepsitory (like described on Development Life blog's post Symfony 2: Injecting service as dependency into doctrine repository), then you can use $this->router->generate('acme_route');

r1pp3rj4ck
  • 1,437
  • 2
  • 10
  • 23
0

in symfony 4 and Sylius when the FormType extends an (ex.) AbstractResourceType

class PostType extends AbstractResourceType
{

    private $router;
    public function __construct(RouterInterface $router, $dataClass, $validationGroups = [])
    {
        $this->router = $router;
        parent::__construct($dataClass, $validationGroups);

    }
}

Services.yaml :

app.post.form.type:
        class: App\Form\Admin\Post\PostType
        tags:
            - { name: form.type }
        arguments: ['@router.default', '%app.model.post.class%' ]
kcompute
  • 1
  • 1