php - use same controller code with multiple views

I am using symfony 5.4 and i have a controller that manage multiple routes and return a view.

Now i would like to create another controller that has the exact same logic in all the pages but that return another view. Here is an exemple of what i have actually :


class KioskController extends AbstractController
{

 public function manualRegistration(
                                ManagerRegistry $doctrine,
                                Request $request, 
                                Session $session,
                                LoggerInterface $logger,
                                MyWebservice      $myWebservice,
                                AppointmentService $appointmentService)
    {
        
       /*
       *  page logic i want to re-use in another in another controller
       */
        return $this->render('path/to/view.html.twig', ['form' => $form->createView(),'myVar'=>$var1, 'myArray'=>$arr]);
    }

    // other routes 

}

My initial idea is to create an abstract class that will extend AbstractController and return an array with all the elements that will be use in the view (form and other variables / arrays) like so :


abstract class AbstractKioskManager extends AbstractController
{

 protected function manageManualRegistration(
                                ManagerRegistry $doctrine,
                                Request $request, 
                                Session $session,
                                LoggerInterface $logger,
                                MyWebservice      $myWebservice,
                                AppointmentService $appointmentService)
    {
        
       /*
       *  page logic i want to re-use in another in another controller
       */
        return  ['form' => $form->createView(),'myVar'=>$var1, 'myArray'=>$arr])
    }

}

and then use this abstract controller for the 2 or more controllers that will display the different views :

class KioskController1 extends AbstractKioskManager
{

 public function manualRegistration(
                                ManagerRegistry $doctrine,
                                Request $request, 
                                Session $session,
                                LoggerInterface $logger,
                                MyWebservice      $myWebservice,
                                AppointmentService $appointmentService)
    {
        
       $viewParams = $this->manageManualRegistration($doctrine,$request,$session,$logger,$myWebservice,$appointmentService);
        return $this->render('path/to/view1.html.twig',$viewParams);
    }

    // other routes

}


class KioskController2 extends AbstractKioskManager
{

 public function manualRegistration(
                                ManagerRegistry $doctrine,
                                Request $request, 
                                Session $session,
                                LoggerInterface $logger,
                                MyWebservice      $myWebservice,
                                AppointmentService $appointmentService)
    {
        
       $viewParams = $this->manageManualRegistration($doctrine,$request,$session,$logger,$myWebservice,$appointmentService);
        return $this->render('path/to/view2.html.twig',$viewParams);
    }

    // other routes

}

Is there a cleaner way to do it ?

Answer

Solution:

Is there a cleaner way to do it ?

Yes. Use Services

Then you just have to inject it to be able to re-use your logic in all the controllers you want, like that :

public function myFunction(Myservice $myService): Response
{
    $myService->doSomething();

    return $this->render('path/to/view1.html.twig');
}

Source