src/PHPDish/Bundle/PostBundle/Controller/PostController.php
public function toggleVoterAction($id)
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');

        $post = $this->getPostManager()->findPostById($id);
        if (!$post) {
            throw new \InvalidArgumentException($this->get('translator')->trans('post.not_exists'));
        }
        if ($isVoted = $post->isVotedBy($this->getUser())) {
            $this->getPostManager()->removeVoter($post, $this->getUser());
        } else {
            $this->getPostManager()->addVoter($post, $this->getUser());
        }
        return $this->json([
            'vote_count' => $post->getVoteCount(),
            'is_voted' => !$isVoted
        ]);
    }
public function toggleVoterAction($id)
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');

        $post = $this->getPostManager()->findPostById($id);
        if (!$post) {
            throw new \InvalidArgumentException($this->get('translator')->trans('post.not_exists'));
        }
        if ($isVoted = $post->isVotedBy($this->getUser())) {
            $this->getPostManager()->removeVoter($post, $this->getUser());
        } else {
            $this->getPostManager()->addVoter($post, $this->getUser());
        }
        return $this->json([
            'vote_count' => $post->getVoteCount(),
            'is_voted' => !$isVoted
        ]);
    }
切换点赞状态

@Route("/posts/{id}/voters", name="post_toggle_voter", methods={"POST"})