4

I would like to access generateUrl in my entity class. You can access generateUrl in controller class like this:

$url = $this->generateUrl('your_route_name', array(/* parameters */));

Accoring do this article, I should try to 'create a service, and inject the router component in it'. Then I am reading Symfony Docs: Service Container and try configuration, but I still can not make it.

in app/config/config.yml

services:
    router:
        class: ???
        arguments: ???

How can I make router as service?

update

Why I want to use generateUrl in Entity class?

I am using Eko/FeedBundle. It requires to implements getFeedItemLink() in Entity. I need to give URL as return value of this function.

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
whitebear
  • 11,200
  • 24
  • 114
  • 237
  • 4
    it's not available inside the entity because you shouldn't use it inside the entity, what are you trying to do? – trrrrrrm Dec 22 '13 at 10:16
  • @ra_htial "you should not question god, nor what the king says" well... why the heck? then you get 2 upvotes. Bravo! – Toskan May 26 '14 at 01:53
  • @Toskan because there are rules if you don't follow things will break apart sooner or later, questioning is good thats why i asked him what he is trying to do, maybe i could helped with a better approach. – trrrrrrm Jun 03 '14 at 07:15

2 Answers2

4

The short answer is: you don't use the router inside entities and you don't create entities as services. You should create a separate service which is responsible for creating urls (if you wrote more about what you are trying achieve it would be simpler to give you more appropriate example).

For example:

use Symfony\Bundle\FrameworkBundle\Routing\Router;

class YourCustomUrlGenerator
{
    private $router;

    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    public function generateUrl(YourEntity $entity)
    {
        // generate the url
    }
}

Then define your service for DIC:

services:
    custorm_url_generator:
        class: Namespace\YourCustomUrlGenerator
        arguments: [@router]
Cyprian
  • 11,174
  • 1
  • 48
  • 45
  • No,I am not trying to make entity as a service. I want to make router as a service then use it in entity class. – whitebear Dec 22 '13 at 11:23
  • 4
    You DON'T use services in entities. It's bad! Router IS a service, you don't need to make it one. – Igor Pantović Dec 22 '13 at 11:29
  • I totally agree. You should not make servce == entity. – Jovan Perovic Dec 22 '13 at 11:48
  • 1
    As @IgorPantović said - you don't use services inside entities. If you need a method which creates an url based on an entity - you create separate service (which IS NOT an entity) and you put this method there. Then you can pass an entity as an argument into this method. In that way you can achieve what you want without putting any service inside entity. – Cyprian Dec 22 '13 at 11:49
  • @IgorPantović Well and you should not have sex before marriage. Unless you can explain why, your answer does not yield any benefits. Like telling a kid "don't touch this" if you cannot explain, I will touch. – Toskan May 26 '14 at 01:51
  • 2
    Joking aside, i didn't make an answer, I made a comment. Entities are Data-Objects which shouldn't be tested, therefore putting business logic inside is a bad idea. Furthermore they will be instantiated in various places in your code, if they require access to services, you will need access to those services in all those places making code more coupled and less maintainable. It violates SRP(but hell, activerecord does it too and most of people don't mind) so whether this is bad is debatable. Factory pattern is the right thing to use here. – Igor Pantović May 26 '14 at 05:38
  • @Toskan -1 rly? :) Aparat from IgorPantovic wrote. How do you want to inject any service into an entity using DIC? You may create service from an entity but it mess up - for example how do you want use repository for such entity (you don't have any option to inject dependency that way)? If you don't understand something, you may just ask, instead of writing stupid things – Cyprian May 26 '14 at 21:10
  • unlike igor you seem to misunderstand. The "stupid" thing I wrote is to point out to you, that giving a "You should not do that" answer is plain bad if you don't give an explanation. Whereas your answer can be super easy to remember, and make a sense out of it, maybe even further develop your answer, if you give a reason. Life improved. – Toskan May 26 '14 at 23:32
  • @Cyprian feel free to edit your answer, then I can turn the -1 into a +1 – Toskan May 26 '14 at 23:33
0

Yes, you can, but you really shouldn't.

This answer explains how to to it: Symfony 2.0 getting service inside entity

Basically you must:

  • create a Listener to the PostLoad Doctrine event
  • add a setter on your entity
  • from the listener pass the dependency to the entity using the setter

The main side effect is that this works only for loaded entities, for example if you create a new instance of the entity, the dependency is null.

In my case I am using this hack to pass the logged user to the entity, this in conjunction with the fact that the PostLoad happens only once, creates strange cases during Symfony application tests with logged users.

So a solution that seems easy and clever, in reality it's a hell that you really should avoid.

nulll
  • 1,465
  • 1
  • 17
  • 28