0

I would first like to say that I saw the other questions on here relating to this error I'm having and none solved my problems.

I have the following code for a controller to check an APIkey before sending data from the backend to the frontend.

file1Controller.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"}) // "/Some/URI" here
     * @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()); //Something here
    }
}

Which works exactly as intended (tested it with Postman and with my browser) for my simple learning example. I would like to generalize it so that I can use it in other places. Almost everything should stay the same except the parts where there are comments. This is what it becomes when making it general:

General.php

<?php


namespace App;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;


class General extends AbstractController
{

    private $route;
    private $entity;

    /**
     * General constructor.
     * @param String $route
     * @param String $entity
     */
    function __construct(String $route, String $entity)
    {
        $this->route = $route;
        $this->entity = $entity;
    }

    /**
     * @Route({$this->route}, 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:{$this->entity}')->findAll());
    }

}

And the file file1Controller.php changes to:

<?php


namespace App\Controller;

use App\General;
use Symfony\Component\HttpFoundation\Request;

class SubscriptionController
{

    /**
     * @return General
     */
    public function AuthenticateAPI()
    {
        $generalObject = new General("/Some/URI", 'Something');
        return $generalObject;
    }

}

This new setup gives no compiler errors but of course, do give the following error (when testing it):

Cannot autowire service "App\General": argument "$route" of method "__construct()" is type-hinted "string", you should configure its value explicitly.

I understand that this error occurs because Symfony doesn't know which String to inject. But there must be a way to get around this? Because I can't specify the value explicitly in my case because I'll be making another file file2Controller.php which will be the exact same but with different $route and $entity.

  • 2
    You need to exclude the General.php file in services.yaml so autowire does not pick it up. Of course it still won't work as desired because merely extending from AbstractController does not give you access to the entity manager. Probably want to review the service container docs to get a better idea of creating services. You have some stuff right but mostly wrong. – Cerad Jun 08 '19 at 22:51
  • 1
    @Cerad I have excluded the file from the services, so thanks for your suggestion. Furthermore, I tried applying the methods from the docs as you hinted but none worked. I've tried using service parameters and manually wiring arguments but errors were just flooding from all over the place. You said I'm doing stuff mostly wrong, but could you please be more specfic on what I could do differently with the above code after I excluded the file in services.yaml? –  Jun 09 '19 at 07:42
  • 1
    Why don't you use a voter? This is one of their use cases – Rufinus Jun 10 '19 at 19:41
  • @Rufinus I could indeed use that but I'm just learning how to make code general and this is not a real app. Just trying to get this specific thing to work to get practice in problem solving. There should be a way around this and I would like to figure it out. –  Jun 11 '19 at 08:17
  • 1
    I ended up making the generalized version a Service and the controller made use of that Service. The complete answer is here: https://stackoverflow.com/a/56543602/11480180 . –  Jun 11 '19 at 13:22

0 Answers0