I have this controller
Controller1.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class file1Controller extends AbstractController
{
/**
* @Route("/Some/URI", methods={"GET"})
* @param Request $request
* @return JsonResponse
*/
public function list(Request $request)
{
if (empty($request->headers->get('api-key'))) {
return new JsonResponse(['error' => 'Please provide an API_key'], 401);
}
if ($request->headers->get('api-key') !== $_ENV['API_KEY']) {
return new JsonResponse(['error' => 'Invalid API key'], 401);
}
return new JsonResponse($this->getDoctrine()->getRepository('App:Something')->findAll());
}
}
Which works exactly as intended (tested it with Postman and with my browser). I would like to generalize to make a general controller as a Service and use that Service for each controller, Controller1.php
, Controller2.php
and Controller3.php
where everything is the same excpet the @route
and the Something
inside the method getRepository
.
This is my go at it:
GeneralService.php
<?php
namespace Service;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class GeneralService
{
/**
* @param Request $request
* @param String $entity
* @return JsonResponse
*/
public function list(Request $request, String $entity)
{
if (empty($request->headers->get('api-key'))) {
return new JsonResponse(['error' => 'Please provide an API_key'], 401);
}
if ($request->headers->get('api-key') !== $_ENV['API_KEY']) {
return new JsonResponse(['error' => 'Invalid API key'], 401);
}
return new JsonResponse($this->getDoctrine()->getRepository('App:{$entity}')->findAll());
}
}
And Controller1.php
is then changed to SubscriptionController.php
:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Service\GeneralService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class SubscriptionController extends AbstractController
{
/**
* @Route("/Some/Uri", methods={"GET"})
* @param GeneralService $generalService
* @param Request $request
* @return JsonResponse
*/
public function AuthenticateAPI(GeneralService $generalService, Request $request)
{
$AuthenticatorObject = $generalService->list($request ,'Something');
return $AuthenticatorObject;
}
}
This unfortunately does not work and yields the following error:
InvalidArgumentException
Cannot determine controller argument for "App\Controller\Controller1::AuthenticateAPI()": the $generalService argument is type-hinted with the non-existent class or interface: "Service\GeneralService".
I don't see where this error is coming from nor why it's happening. Could somehelp help me understand why is this the case and how to fix it?