【问题标题】:How get request parameter in Symfony Voter?如何在 Symfony Voter 中获取请求参数?
【发布时间】:2017-05-12 21:34:06
【问题描述】:

我设置了 request_stack 参数

api.uid_voter:
    class: SecretBundle\Security\UidVoter
    arguments: ['@request_stack']
    tags:
        - { name: security.voter }
    public: false

#app/config/security.yml

access_control:
    - { path: ^/secret, roles: [UID] }

接下来我在 Voter 中尝试

public function __construct(RequestStack $request_stack)
{
    $this->uid = $request_stack->getCurrentRequest()->get('uid');
}

在 null 上调用成员函数 get()

但是,如果 $this->denyAccessUnlessGranted('UID') 来自控制器 - 没有错误

【问题讨论】:

  • 我需要简单地将来自 url 的 UID 与令牌中的授权用户 ID 进行比较

标签: symfony


【解决方案1】:

创建服务本身时,实际请求不可用。当您真正需要它时,您需要获取 uid。比如:

class MyVoter {
    private $requestStack;
    public function __construct($requestStack) {
        $this->requestStack = $requestStack;
    }
    private function getUid() {
        return  $this->requestStack->getMasterRequest()->get('uid');

【讨论】:

    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 2016-12-20
    • 2017-10-21
    • 2021-01-12
    • 2020-06-12
    • 1970-01-01
    相关资源
    最近更新 更多