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.

3
  • 4
    it's not available inside the entity because you shouldn't use it inside the entity, what are you trying to do? Commented Dec 22, 2013 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! Commented May 26, 2014 at 1: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. Commented Jun 3, 2014 at 7:15

2 Answers 2

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] 
Sign up to request clarification or add additional context in comments.

9 Comments

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.
You DON'T use services in entities. It's bad! Router IS a service, you don't need to make it one.
I totally agree. You should not make servce == entity.
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.
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.
|
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.

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.