Search code examples
symfony4phpspreadsheet

How can I call a function?


I have a function in a class in App/src/utils. I have issue using it from a controller.

I have to construct some sheets with phpspreadsheet. The method length is 260 lines. I don't think it's a good practice to have it in a function of the controller. So, I try to put it in a class (ExcelCreate) I have created in App/Utils, in a public function called excelfrais, which create the sheet, using an array, called $data, and 2 variables called $user and $nblig. The function save the sheet, and send it to the browser. I put a "use App\Utils\ExcelCreate;" in my controller I call the function from the controller.

    use App\Utils\ExcelCreate;
    ...
class ComiteController extends Controller
    {
    public function frais(Request $request, $nblig)
        { ...
                $data=$form->getData();
                $nblig=$data['nblig'];
                excelfrais($user,$data,$nblig); ...
        }}

And there is my issue : I have an Exception : Attempted to call function "excelfrais" from namespace "App\Controller"


Solution

  • I had a big search. Reading carefully the Symfony documentation I found there : https://symfony.com/doc/current/best_practices/business-logic.html the solution : in the function of my controller, I need to have a use of the class whose the function is a property :

    public function frais(Request $request, ExcelCreate $create, $nblig)
        {...}
    

    and the call to this function should be

    $create->excelfrais($user,$data,$nblig);