-2

I want to add my function handleParameters() into my AuthController but I had "Expected to find a class ... while importing services from resource... but it was not found!"

HandleParameters.php

<?php 
   function handleParameters(array $aRequired, &$aRes, $requestMethod) {...}
?>

AuthController.php

<?php
class AuthController extends Controller 
{
   public function getAuth() 
   {
     require_once (dirname(__DIR__, 1) .'/Common/HandleParameters.php');
     handleParameters($aRequired, $this->aRes, $_SERVER['REQUEST_METHOD']);
     ...
   }
}
yivi
  • 42,438
  • 18
  • 116
  • 138
  • 2
    The message is coming from autowire which, by default, attempts to open up any php files it encounters and wire up the class inside. Just add HandleParameters.php to the ignore section in config/services.yaml. And while off topic, there is really no reason for doing what you are doing in a Symfony application. – Cerad Nov 25 '19 at 16:05
  • I'm learning Symfony, so if you have other better solutions, I'm willing to hear from you. I can put handleParameters function into AuthController but because it's a shared function for other webservices classes so I want to separate it to another file. – Zita Nguyen Nov 26 '19 at 07:51

2 Answers2

0

All your php file must be a class by default so if you have the error

"Expected to find a class"

It's because your HandleParameters.php isn't a class

You have two solutions.

  • If the function is specific to your AuthController you can just define it in your controller class the same way you are defining getAuth().

  • Otherwise: The other way is to define a specific class MyService as a service and then you can inject that service into the contructor of any class, controller you want.

Minirock
  • 628
  • 3
  • 13
  • 28
0

You can use trait, for example:
- create a Tool/ folder in src/.
- create in this folder a file HandleParametersTrait.php, the name must end with Trait.

HandleParametersTrait.php

<?php

namespace App\Tool;

trait HandleParametersTrait {
  public function handleParameters($message) {
    echo $message;
  }
}



AuthController.php

<?php

namespace App\Controller;

use App\Tool\HandleParametersTrait;

class AuthController extends Controller 
{
   use HandleParametersTrait;

   public function getAuth() 
   {
     $this->handleParameters('hi');
     ...
   }
}
  • Thank you Yacine. I thought of it, but my chef wants it to be a simple php function, so I'm working around to find a solution. – Zita Nguyen Nov 26 '19 at 07:53