현재 Symfony 양식을 배우고 있습니다. 문제가있는 코드는 내가 원하는대로 작동하지 않습니다. 내가하려는 것은 사용자가 내 기본 컨트롤러에 데이터를 제출하도록하는 것입니다. 데이터가 유효하고 다른 방법으로 리디렉션되도록하여 해당 데이터를 데이터베이스에 제출하는 것과 같은 작업을 수행 할 수 있습니다.리플렉션을 통해 Symfony 3 객체 전달
내가 겪고있는 문제는 간단한 이름과 날짜 필드가있는 작업 개체 인 다른 데이터 경로를 전달하는 것입니다.
내가 작성한 코드 중 일부는 구조의 클래스 종류와 같은 기본 형식입니다. 당신이 볼 수있는 TaskType.php
namespace AppBundle\Entity\Forms;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use AppBundle\Entity\Forms\Task;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('task', TextType::class)
->add('dueDate', DateType::class, array('label' => 'Register'))
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Task::class,
));
}
DefaultController.php
... 그래서
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Forms\TaskType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
...
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$form = $this->createForm(TaskType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$task = $form->getData();
// ... perform some action, such as saving the task to the database
// for example, if Task is a Doctrine entity, save it!
// $em = $this->getDoctrine()->getManager();
// $em->persist($task);
// $em->flush();
return $this->redirectToRoute('success', array($task));
}
return $this->render('forms/task.html.twig', array(
'form' => $form->createView(),
));
}
/**
* @Route("/success", name="success")
*/
public function successAction(Request $request) {
dump($request);die;
}
가, 난 그냥 배열로 작업을 전달하고있어
. 이 코드에서는 요청을 통해 작업에 액세스 할 수 있다고 생각했지만 작동하지 않는 것 같습니다. 그리고 successAction에 Task [] $ task 매개 변수를 추가하려고하면 작업을 찾을 수 없습니다.
두 번째 작업에서 개체에 액세스 할 수있는 방법이 있습니까? 아니면 전 여기서 거슬러 올라갈 수 있을까요? 리디렉션을 수행하기 전에 데이터 삽입 또는 다른 데이터 조작을 수행해야합니까? 아니면 거기에 데이터를 처리 할 수 있도록 컨트롤러의 다른 메서드에 작업 개체를 전달하는 방법은 무엇입니까? 귀하의 의견
여러분의 성공 조치가 작업 매개 변수 (id를 또는 무엇이든)를 사용해야합니다. http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html – COil