api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.41k stars 861 forks source link

Custom Route Issue with IRIConverter @Id #2864

Closed pollend closed 3 years ago

pollend commented 5 years ago

I have a custom route that I use to retrieve a collection of items but the route fails at IRIconverter. @id type is suppose to generate a route for the collection but the route breaks because the parameters are not in the RequestContext. Below is a workaround that I had to implement to resolve the problem. Not sure if this is a bug exactly.

I set parameters manually in the context:

$this->context->setParameter('session_id',$session_id);
$this->context->setParameter('page',$page);
class GetQuizQuestionBySessionAndPage
{

    private $entityManager;
    private $security;
    private  $context;
    public function __construct(Security $security, EntityManagerInterface $entityManager, RequestContext $context)
    {
        $this->entityManager = $entityManager;
        $this->security = $security;
        $this->context = $context;
    }

    public function __invoke(Request $request,$session_id, $page)
    {

        /** @var QuestionRepository $questionRepo */
        $questionRepo = $this->entityManager->getRepository(QuizQuestion::class);

        /** @var QuizSessionRepository $sessionRepo */
        $sessionRepo = $this->entityManager->getRepository(QuizSession::class);

        /** @var QuizSession $session */
        if ($session = $sessionRepo->find($session_id)) {
            $quiz = $session->getQuiz();
//$this->security->isGranted($session, QuizSessionVoter::VIEW) &&
            if ($session->getCurrentPage() == $page) {

                $groups = $questionRepo->getUniqueGroups($quiz);
                $questions = $questionRepo->filterByGroup($groups[$page], $quiz);
//                ['questions' =>
                $this->context->setParameter('session_id',$session_id);
                $this->context->setParameter('page',$page);

                return   new Collection($questions);//, 'maxPage' => Collection::make($groups)->keys()->max()];
            }

        }
        throw new \Exception("not found");
    }
}
soyuka commented 3 years ago

Greetings! We appreciate your concern but weren't able to reproduce this issue or it is more of a question. As described in the API Platform contributing guide, we use GitHub issues for bugs and feature requests only.

For support question ("How To", usage advice, or troubleshooting your own code), you have several options:

Feel free reach one of the support channels above. In the meantime we're closing this issue.