我如何在 Symfony 4 中概括 ApiKeyAuthenticator?

How can I generalize an ApiKeyAuthenticator in Symfony 4?

我有以下代码在将数据发送到前端之前检查 API-key 是否正确。

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
    }
}

对于我的简单学习示例,它可以按预期正常工作(使用 Postman 对其进行测试)。我想概括它,以便我可以在其他地方使用它。除了有评论的部分,几乎所有内容都应该保持不变。我尝试了以下方法:

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 $request;
    private $route;
    private $entity;

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

    /**
     * @Route({$route}, methods={"GET"}) //notice here
     * @return JsonResponse
     */
    public function list()
    {
        if (empty($this->request->headers->get('api-key'))) {
            return new JsonResponse(['error' => 'Please provide an API_key'], 401);
        }

        if ($this->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()); //notice here
    }

}

那我把file1Controller.php的代码改成:

<?php


namespace App\Controller;

require(__DIR__.'/../General.php'); //note that there's no error accessing the file here
use Symfony\Component\HttpFoundation\Request;

class file1Controller
{

    /**
     * @param Request $request
     */
    public function AuthenticateAPI(Request $request)
    {
        $AuthenticatorObject = new ApiKeyAuthenticator($request, "/Some/URI", 'Something'); //getting undefiend class 
        return $AuthenticatorObject;
    }

}

不幸的是,这在使用 Postman 进行测试时无法正常工作,我在 file1Controller.php

中的 $AuthenticatorObject = new ApiKeyAuthenticator($request, "/Some/URI", 'Something'); 这一行遇到了不安全的 class 错误

我做错了什么,我该如何解决?

你不应该在 Symfony 中这样调用你的控制器:

require(__DIR__.'/../General.php'); //note that there's no error accessing the file here

请查看在 Symfony 文档中定义和访问控制器作为服务:

How to Define Controllers as Services

如何将请求转发给另一个控制器