Reputation: 10926
I have a bit of a dilema, I have a Symfony2 app and in it I've built a bundle which is just a REST Api layer to my database.
The thing is that I have another bundle, and I want it to perform some updates in the database. I don´t want to rewrite code to perform the same tasks in this new bundle.
Is it feasible for me to issue requests to my api from another bundle within the same app? Would it take longer than making the queries from this new bundle? I'm concerned about performance and scalability.
To exemplify I'll write an example:
Bundle A contains a REST api, one of the resources that it exposes is "Person" which allows GET, POST, PUT, DELETE. This resource maps to a database table.
In the other hand, there is Bundle B, which has to run some tasks and finally update some users in my database. I don't want to copy my Person Entity from the api bundle to this bundle to perform the update.
What would you do in this situation?
Upvotes: 1
Views: 195
Reputation: 339
I think You should communicate between bundles using services (Dependency Injection).
If you register service in one bundle, and you named it "myDataLayerService", you can inject it into services of another bundle(Like any other services - request service, entity manager, router, etc.) or, you can get it in controller very easilly:
$myDBLayer = $this->get("myDataLayerService");
And then you call any public function created in your service.
$myDBLayer->persistObjectToDatabase($veryNiceObject);
TL;DR: Registered Symfony services in one bundle, are available in any other bundle.
Upvotes: 1