如何在 Symfony 4 中概括 ApiKeyAuthenticator?

我爱PHP

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

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 对其进行测试时,这不起作用,并且我在此行$AuthenticatorObject = new ApiKeyAuthenticator($request, "/Some/URI", 'Something');遇到了 undefiend 类错误file1Controller.php

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

阿里·卡泽米

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

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

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

如何将控制器定义为服务

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在 Symfony 4 中实现处理程序?

来自分类Dev

如何在 Symfony 4 中通过 GET 请求访问属性?

来自分类Dev

如何在symfony4的表单类中隐藏标签?

来自分类Dev

如何在 Symfony 4 中制作喜欢和不喜欢?

来自分类Dev

如何在 Symfony 4 的功能测试中验证用户身份

来自分类Dev

如何防止 Symfony 4 中的 ParseError?

来自分类Dev

如何替换 Symfony 4 函数中的变量?

来自分类Dev

如何在numpy中概括元素级矩阵运算

来自分类Dev

如何在Haskell中概括来自URL和文件的读取

来自分类Dev

如何在numpy中概括元素级矩阵运算

来自分类Dev

如何在Symfony 4中使用libphonenumber.phone_number_util

来自分类Dev

如何在 symfony 4 中获取关系数据库的 id?

来自分类Dev

如何在 Symfony 4 中获得有效的类?

来自分类Dev

如何在symfony 4 api中获取仅显示选定数据的sql请求

来自分类Dev

如何在 Symfony4 中对 Max 和 Min 进行搜索查询?

来自分类Dev

如何在 Symfony 4.x 中为“可重用包”配置“实体”?

来自分类Dev

Symfony 4 中的注解

来自分类Dev

如何从 Symfony 4 中的数组在实体中创建新条目?

来自分类Dev

如何安装 Symfony 4 而不是 Symfony 3.4?

来自分类Dev

如何在 symfony 4 中以下拉菜单的形式显示从关系数据库中获取的数据的表单?

来自分类Dev

如何从Symfony 4中的日志中排除弃用消息?

来自分类Dev

如何从Symfony4中的选民那里返回更多信息?

来自分类Dev

如何在Symfony中添加Ajax Capabilites

来自分类Dev

如何在symfony 2中捕获Exception?

来自分类Dev

如何在symfony 2中捕获Exception?

来自分类Dev

如何在Symfony中强制注销用户?

来自分类Dev

如何在Symfony 2.1中显示查询

来自分类Dev

如何在Symfony中多次嵌入表单

来自分类Dev

如何在Symfony 5中编码密码?

Related 相关文章

  1. 1

    如何在 Symfony 4 中实现处理程序?

  2. 2

    如何在 Symfony 4 中通过 GET 请求访问属性?

  3. 3

    如何在symfony4的表单类中隐藏标签?

  4. 4

    如何在 Symfony 4 中制作喜欢和不喜欢?

  5. 5

    如何在 Symfony 4 的功能测试中验证用户身份

  6. 6

    如何防止 Symfony 4 中的 ParseError?

  7. 7

    如何替换 Symfony 4 函数中的变量?

  8. 8

    如何在numpy中概括元素级矩阵运算

  9. 9

    如何在Haskell中概括来自URL和文件的读取

  10. 10

    如何在numpy中概括元素级矩阵运算

  11. 11

    如何在Symfony 4中使用libphonenumber.phone_number_util

  12. 12

    如何在 symfony 4 中获取关系数据库的 id?

  13. 13

    如何在 Symfony 4 中获得有效的类?

  14. 14

    如何在symfony 4 api中获取仅显示选定数据的sql请求

  15. 15

    如何在 Symfony4 中对 Max 和 Min 进行搜索查询?

  16. 16

    如何在 Symfony 4.x 中为“可重用包”配置“实体”?

  17. 17

    Symfony 4 中的注解

  18. 18

    如何从 Symfony 4 中的数组在实体中创建新条目?

  19. 19

    如何安装 Symfony 4 而不是 Symfony 3.4?

  20. 20

    如何在 symfony 4 中以下拉菜单的形式显示从关系数据库中获取的数据的表单?

  21. 21

    如何从Symfony 4中的日志中排除弃用消息?

  22. 22

    如何从Symfony4中的选民那里返回更多信息?

  23. 23

    如何在Symfony中添加Ajax Capabilites

  24. 24

    如何在symfony 2中捕获Exception?

  25. 25

    如何在symfony 2中捕获Exception?

  26. 26

    如何在Symfony中强制注销用户?

  27. 27

    如何在Symfony 2.1中显示查询

  28. 28

    如何在Symfony中多次嵌入表单

  29. 29

    如何在Symfony 5中编码密码?

热门标签

归档