简体   繁体   English

如何在 Symfony 5 中获取 kernel 响应中的用户?

[英]How to get User in kernel response in Symfony 5?

I am making simple api wrapper, so all requests to https://example.comA/api/me must be catched on kernel response level and forwarded to https://api.example.comB/me and all was fine however I cannot get the currently logged in User in that kernel response because it returns null: I am making simple api wrapper, so all requests to https://example.comA/api/me must be catched on kernel response level and forwarded to https://api.example.comB/me and all was fine however I cannot在 kernel 响应中获取当前登录的用户,因为它返回 null:


namespace App\Manager\Api\Event;

use App\Provider\Core\Api\CoreApi;
use GuzzleHttp\Exception\BadResponseException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Security\Core\Security;

class ApiWrapperEventListener
{
    private $coreApi;
    private $security;

    public function __construct(CoreApi $coreApi, Security $security)
    {
        $this->coreApi = $coreApi;
        $this->security = $security;
    }

    public function onKernelResponse(ResponseEvent $event)
    {
        if (!$event->isMasterRequest()) return;

        $request = $event->getRequest();

        if ('/api' === substr($request->getPathInfo(), 0, 4)) {
            dump($this->security->getUser()); // returns NULL
            die;

            try {
                $response = $this->coreApi->call($request->getMethod(), $request->getPathInfo(), json_decode($request->getContent(), true) ?? []);
                $event->setResponse(new JsonResponse($response));
            } catch (BadResponseException $error) {
                dump($error);
                die;
            }
        }
    }
}

I guess Symfony is firing those events before I get the User, is there a way to get this right?我猜 Symfony 在我得到用户之前触发了这些事件,有没有办法解决这个问题?

I have to note that in other places like controllers or services I get the User right.我必须注意,在控制器或服务等其他地方,我得到了用户的权利。

Ok i know what was the problem.好的,我知道问题出在哪里。

The response controller event has no User when 404 or 500 given.当给出 404 或 500 时,响应 controller 事件没有用户。

In my case I was catching 404, passing to listener and modifying the request to 200.在我的情况下,我捕获了 404,传递给侦听器并将请求修改为 200。

This approach wasn't good, so i decided to move this to Controller itself.这种方法不好,所以我决定把它移到 Controller 本身。

    /**
     * @Route("/api/v1/{uri}", name="api", requirements={"uri"=".+"})
     */
    public function api(
        Request  $request,
        CoreApi  $coreApi
    ):Response
    {
        try
        {
            $response = $coreApi->call($request->getMethod(), $request->getPathInfo(), json_decode($request->getContent(), true) ?? []);
            return new JsonResponse($response);
        }
        catch(BadResponseException $error)
        {
            return new Response($error->getResponse()->getBody()->getContents(), $error->getResponse()->getStatusCode());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM