<?php
# src/Controller/ParentController.php
namespace App\Controller;
use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FatherController extends AbstractController
{
public function __construct(private EntityManagerInterface $entityManager, private LoggerInterface $logger) {}
#[Route('/father', name: 'app_father')]
public function index(): Response
{
$products = $this->entityManager->getRepository(Product::class)
->findBy([], ['name' => 'ASC']);
if (empty($products)) {
$this->logger->error('Products list is empty');
}
return $this->json([
'products' => $products,
]);
}
}
<?php
# src/Controller/ChildController.php
namespace App\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
class ChildController extends FatherController
{
public function __construct(private EntityManagerInterface $entityManager, private LoggerInterface $logger, private MailerInterface $mailer)
{
parent::__construct($this->entityManager, $this->logger); // <= use parent to instantiate FatherClass
}
#[Route('/child', name: 'app_child')]
public function index(): Response
{
$this->mailer->send(/** ... */);
return $this->json([
// ...
]);
}
}