【问题标题】:Handling return form and redirect in service in Symfony5在 Symfony5 中处理返回表单和重定向服务
【发布时间】:2022-01-04 11:52:12
【问题描述】:

想要将表单作为服务处理,因为我需要在几个控制器中使用它们

App\Controller

$todo = new Todo();

$todo
    ->setOwner($this->getUser())
    ->setCreationDate(new \DateTime());
$form = $this->createForm(TodoType::class, $todo, [
    'userId' => $this->getUser(),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
    $entityManager->persist($todo);
    $entityManager->flush();
    
    return $this->redirectToRoute('todos');
} 


return $this->render('todo/todos.html.twig', [
    'form' => $form->createView(),
]);

所以我把它投入使用,它看起来像这样



class CreateTodo
{
    private $security;
    private $entityManager;
    private $formFactory;
    private $request;
    private $router;

    public function __construct(Security $security, EntityManagerInterface $entityManager, FormFactoryInterface $formFactory, RequestStack $request, RouterInterface $router)
    {
        $this->security = $security;
        $this->entityManager = $entityManager;
        $this->formFactory = $formFactory;
        $this->request = $request;
        $this->router = $router;
    }
    
    public function createTodo() {
        $todo = new Todo();
        
        $todo
            ->setOwner($this->security->getUser())
            ->setCreationDate(new \DateTime());
        $form = $this->formFactory->create(TodoType::class, $todo, [
            'userId' => $this->security->getUser(),
        ]);
        $form->handleRequest($this->request->getCurrentRequest());
        if ($form->isSubmitted() && $form->isValid())
        {
            $this->entityManager->persist($todo);
            $this->entityManager->flush();
            
            return $this->router->generate('todos');
        } 
        return $form;
    }
}

问题是我无法提交表单,因为$form->createView() 不存在,如果我将return $this->router->generate('todos') 替换为return $form,它不会重定向我,因此在我刷新之前它不会更新。如果我要在多个控制器中使用表单,将它们移动到服务中是否也是一个好主意?

【问题讨论】:

  • 您可能正在寻找embedded controllers。如果这种方法不可行,那么至少您必须检查 createToDo 方法的返回值。我想你可能想多了。
  • 所以要明确一点,不值得把这种“表单处理”方法放到服务中吗?如果是这样,有没有好办法让这个表单可重复使用?

标签: php symfony


【解决方案1】:

我遇到了同样的问题,一种可能性是使用带有 symfony3 embedded controller with form 的嵌入式控制器来获取数据。但是,你不能从那里重定向,因为它在 Twig 中......

您可以做的是将表单提交的主要逻辑放在服务中,使表单在显示它们的控制器中。然后在它自己的模板中添加表单 HTML 并将其导入到 Twig 视图中。

src\Controller\Todo.php

public function index(CreateTodo $createTodo, Request $request): Response
    $form = $this->createForm(TodoType::class);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $createTodo->createTodo($form);

        return $this->redirectToRoute('todos');
    }

    return $this->render('todo/todos.html.twig', [
        'form' => $form->createView(),
    ]);
}



src\Service\CreateTodo.php

public function createTodo() {
    $todo = new Todo();
    
    $todo
        ->setOwner($this->security->getUser())
        ->setCreationDate(new \DateTime());
    
        $this->entityManager->persist($todo);
        $this->entityManager->flush();  
    } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多