EasyAdmin3: How to inject services into CrudControllers

It is said:

Technically, these CRUD controllers are regular Symfony controllers so
you can do anything you usually do in a controller, such as injecting
services and using shortcuts like $this->render() or $this->isGranted().

So I tried this to adapt my EDIT-page:

class CourseCrudController extends AbstractCrudController {

    private $personRepository;

    public function __construct(PersonRepository $personRepository) {
        dump("Can you see me?");
        $this->personRepository = $personRepository;
    }

    public function configureFields(string $pageName): iterable {
        // ...

        $id = 1
        $person = $this->personRepository->find($id); // throws: Call to a member function find() on null

        // ...
    }

    // ...
}

Question

How do I access other Repositories/Services in a CRUD-Controller?

The constructor seems not being executed…

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *