[英]Symfony RedirectToRoute with array paramater
I'm working on an upload system based on Symfony 4 and PHPSpreadsheet.我正在开发基于 Symfony 4 和 PHPSpreadsheet 的上传系统。 My user uploads the excel file.
我的用户上传了 excel 文件。 I then create the products/categories... At the end I want to get all categories created.
然后我创建产品/类别...最后我想创建所有类别。 The following Doctrine query:
以下 Doctrine 查询:
/**
* @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()
;
}
gets all my categories where created_by
is by the user ID, and where Parent is null.获取我的所有类别,其中
created_by
是用户 ID,Parent 是 null。
What I want to do is to get an array of all those categories and then redirect the user to a rendered Twig template page where he can make the link.我想要做的是获取所有这些类别的数组,然后将用户重定向到呈现的 Twig 模板页面,他可以在其中创建链接。 But I don't really understand how to use the parameters... In my Upload I've set this:
但我不太明白如何使用参数......在我的上传中我设置了这个:
$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());
}
I don't understand how I can use the request to redirect the user correctly using the route:我不明白如何使用请求使用路由正确重定向用户:
/**
* @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'));
}
Well I suppose I have to add $isNew
as a parameter for my request?好吧,我想我必须为我的请求添加
$isNew
作为参数? But did I reuse the array after to use this in my twig, and display the element from this array inside my twig.但是在我的 twig 中使用它之后,我是否重用了该数组,并在我的 twig 中显示了该数组中的元素。
There is a small error:有个小错误:
If your route is "/admin/categories/import" and you want to transfer a value like "$this->getUser()->getId()" then this should be like如果您的路线是“/admin/categories/import”并且您想传输像“$this->getUser()->getId()”这样的值,那么这应该是
Route "/admin/categories/import/{userId}" and your return would be:路由“/admin/categories/import/{userId}”,您的回报将是:
return $this->redirectToRoute('admin.categorie.link', ['userId' => $this->getUser()->getId()]);
and your controller could take the userId as var:并且您的 controller 可以将 userId 作为 var:
/**
* @Route("/admin/categories/import/{userId}", name="admin.categorie.link", methods={"GET|POST"})
*/
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]);
}
Now you can access your categories in twig with {{categories}}现在您可以使用 {{categories}} 访问 twig 中的类别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.