G
G
grabbee2017-05-12 15:51:22
symfony
grabbee, 2017-05-12 15:51:22

Symfony voters, why does passing $request_stack cause an error?

I'm trying to figure out Voters and check the user ID to match the $uid from the request. It seems logical to pass the $request_stack to the service But there is a problem
# app/config/security.yml
* If I specify the water attribute in the access_control, I get the error "Trying to get property of non-object" when I try to get the $uid request parameter $request_stack ->getCurrentRequest()->get('uid');

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

Found that the water constructor is called for some reason twice, and in the first $request_stack->getCurrentRequest() === null trying to request ->get('uid') and causes an error
api.uid_voter:
        class: SecretBundle\Security\UidVoter
        arguments: ['@request_stack']
        tags:
            - { name: security.voter }
        public: false

Now wrapped with a crutch
public function __construct(RequestStack $request_stack)
    {
        $request = $request_stack->getCurrentRequest();
        if ($request) {
            $this->uid = $request->get('uid');
        } 
    }

Maybe someone knows what is the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2017-05-12
@grabbee

1. You need a method getMasterRequest()
2. You need to receive the object $requestduring the execution of the service methods, and not in the constructor
3. You if ($request) {still have to wrap it in, because, for example, there will be no request in the console

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question