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 :)

1

4 Answers 4

18

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)

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

4 Comments

But $this->container is also not available in repositories. How do i pass container to them?
You completely missed the point of the question in this answer!! Use of ""this" assumes that you're inside the controller.
@cartbeforehorse no, this asumes assumes your in an object which can be instantiated...
@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.
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.

6 Comments

injecting just router is also a bad idea?
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, perhaps some links to the documentation would help
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.
the way that you cannot define what kind of interface is returned makes it actually not really easier to understand for a developer
|
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');

1 Comment

Your link is dead.
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%' ] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.