Symfony RedirectToRoute具有数组参数

问题描述 投票:0回答:1

我正在基于Symfony 4和PHPSpreadsheet的上传系统上工作。我的用户上传了excel文件,然后创建了产品/类别...最后,我想创建所有类别,这样做,我得到了所有类别,其中created_by是用户ID,而Parent是空值。所以我的学说查询看起来像这样:

    /**
     * @param $user_id
     * @return array
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function checkIfNew($user_id): ?array {
        return $this->createQueryBuilder('c')
            ->andWhere('c.categorie_parent_id is NULL')
            ->andWhere('c.created_by = :val')
            ->setParameter('val', $user_id)
            ->getQuery()
            ->getResult()
            ;
    }

[我想做的是,您将获得所有这些类别的数组,然后将用户重定向到可创建链接的树枝。但是我不太了解如何使用参数...在我的上传中,我已经设置了:

$isNew = $this->getDoctrine()
                        ->getRepository(Categorie::class)
                        ->checkIfNew($this->getUser()->getId());
                if (!is_null($isNew)){
                    $this->addFlash('success', 'Catalogue crée avec succès');
                    return $this->redirectToRoute('admin.categorie.link',
                        $this->getUser()->getId());
                }

我不明白如何使用请求通过路由正确重定向用户:

    /**
     * @Route("/admin/categories/import", name="admin.categorie.link", methods={"GET|POST"})
     * @param Categorie $categories
     */
    public function linkImport()
    {
        // What TODO here ?
        return $this->render('admin/categories/link.html.twig', compact('categories'));
    }

我想我必须为请求添加$isNew作为参数?但是我是否在使用后在数组中重用了数组,并在树枝中显示了数组中的元素。

php symfony
1个回答
0
投票

有一个小错误:

如果您的路线是“ / admin / categories / import”,并且您想要传输“ $ this-> getUser()-> getId()”之类的值,则应类似于

路由“ / admin / categories / import / {userId}”,您的返回将是:

return $this->redirectToRoute('admin.categorie.link', ['userId' => $this->getUser()->getId()]);

并且您的控制器可以将userId作为var:

public function linkImport(int $userId, EntityManagerInterface $em) // var name must be the same as in the route
{
    // What TODO here ?
    $categories = $em->getRepository(Categories::class)->findByUserId($userId);

    return $this->render('admin/categories/link.html.twig', ['categories' => $categories]);
}

现在您可以使用{{categories}}访问树枝中的类别了>

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.