Answer the question
In order to leave comments, you need to log in
Should I move getting ORM EntityManager to __constructor?
Should I move getting ORM EntityManager in controller to __constructor ?
Here is an example of code, most of which is duplicated (part of the standard CRUD). Previously, I took out such pieces in the constructor. And how to do this in a symphony?
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApiBundle:Upload')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Upload entity.');
}
//...
}
/**
* Displays a form to edit an existing Upload entity.
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApiBundle:Upload')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Upload entity.');
}
//...
}
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PhotoController extends Controller
{
public function __construct(EntityManager $entityManager)
{
//...
}
services:
api.manager:
class: ApiBundle\Controller\PhotoController
arguments: [ @doctrine.orm.entity_manager ]
There are mainly only disadvantages to using property injection, it is similar to setter injection but with these additional important problems:
You cannot control when the dependency is set at all, it can be changed at any point in the object's lifetime.
You cannot use type hinting so you cannot be sure what dependency is injected except by writing into the class code to explicitly test the class instance before using it.
Answer the question
In order to leave comments, you need to log in
/**
* @Route("/url/{id}")
*/
public function editAction(Upload $upload)
{
...
}
1. Since you have decided to make the controller a service, go here symfony.com/doc/current/cookbook/controller/servic...
2. Make the repository a service.
(They forgot about routing.)
3. ParamConverter
4. Inherit from the abstract controller and implement the "smarter" method to get the repository
You can also use docs.sylius.org/en/latest/bundles/SyliusResourceBundle
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question