一个表单在多页面上使用,具有一个控制器操作

时间:2017-08-08 09:15:41

标签: forms symfony optimization controller

我正在与Symfony 3合作,我有一个"优化"问题:

  • 我有一个表格
  • 此表单用于ONE controller
  • 但此表格用于两页

是否可以为TWO页面创建视图+处理此用户的表格一次性?

现在我看到的唯一解决方案是为每个页面复制我的代码两次(=我的控制器中的两个操作)

有什么建议吗?

**
* @Route("/blabla")
*/
class MonController extends Controller
{
/**
 * @Route("/blabla")
 *
 * some stuff
 */
public function homePageAction(Entity $entity, Request $request)
{
    $form = $this->createForm(DateType::class);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $data  = $form->getData();

        dump($data);
    }

    return $this->render(':directory:file.html.twig', [
        'entity' => $entity,
        'form'      => $form->createView()
    ]);
}

/**
 * @Route("/blabla")
 *
 * some stuff
 *
 */
public function detailAction(Entity $entity)
{
      // I need to do the same here to show and handle my form         

      return $this->render('mon template');
  }
}

2 个答案:

答案 0 :(得分:0)

好。因此,您可以将两个路径都放在一个方法中。您的注释将合并在一起,如下所示:

/**
 * @Route("/{slugHoroscope}", name="horoscope_homepage")
 * @Route("/{slugHoroscope}/{slugZodiac}.{_format}", name="horoscope_detail", defaults={"_format": "html"})

将你的参数放在相同的注释中。

然后将params添加到您将要使用的任何方法中。所以像这样:

public function homePageAction(Horoscope $horoscope = null, HoroscopeSigne $horoscopeSigne = null, Request $request)
{
    $form = $this->createForm(DateType::class);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $data  = $form->getData();

        dump($data);
    }
}

然后你将不得不在路线和参数上做一些逻辑。类似的东西:

$request = $this->container->get('request');
$routeName = $request->get('_route');

if ($routeName == 'horoscope_homepage) {
   $template = the_template_for_the_homepage;
   $params = the_parameters_for_the_homepage;
} else {
   $template = the_template_for_the_other_page;
   $params = the_params_for_the_other_page;
}

然后你还必须对params做一些逻辑。类似的东西:

if ($horoscope == null) {
   //your logic
}

然后返回你的模板和参数。像这样:

return $this->render($template, $params);

你必须摆弄很多逻辑。另一种选择是将表单逻辑提取到另一种方法或服务。

如:

<?php
namespace AppBundle\Controller;

use...

**
* @Route("/horoscopes")
*/
class HoroscopeController extends Controller
{
/**
 * @Route("/{slugHoroscope}", name="horoscope_homepage")
 *
 * @ParamConverter("horoscope", class="AppBundle:Horoscope", options={
 *    "mapping": {"slugHoroscope": "slug"},
 *    "repository_method" = "findByComplexSlug",
 *    "map_method_signature" = true
 * })
 */
public function homePageAction(Horoscope $horoscope, Request $request)
{
    $this->formMethod();

    return $this->render(':horoscope:acc-horoscope.html.twig', [
        'horoscope' => $horoscope,
        'form'      => $form->createView()
    ]);
}

/**
 * @Route("/{slugHoroscope}/{slugZodiac}.{_format}", name="horoscope_detail", defaults={"_format": "html"})
 *
 * @ParamConverter("horoscopeSigne", class="AppBundle:HoroscopeSigne", options={
 *    "mapping": {"slugHoroscope": "slugHoroscope", "slugZodiac": "slugZodiac"},
 *    "repository_method" = "findByComplexSlug",
 *    "map_method_signature" = true
 * })
 */
public function detailAction(HoroscopeSigne $horoscopeSigne)
{

    $this->formMethod();        

     $template = $horoscopeSigne
        ->getHoroscope()
        ->getHoroscopeModele()
        ->getTemplatePath();

    return $this->render('horoscope/'.$template, [
        'horoscope' => $horoscopeSigne->getHoroscope(),
        'horoscopeSigne' => $horoscopeSigne,
    ]);
}

private function formMethod() {
    $form = $this->createForm(DateType::class);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $data  = $form->getData();

        dump($data);
    }

}

答案 1 :(得分:0)

主要开发人员设置的最终解决方案是创建一个服务来处理表单&#34;解决方案&#34; (星座)并在控制器中执行此操作:

/**
 * @Route("/{slugHoroscope}", name="horoscope_homepage")
 *
 * @ParamConverter("horoscope", class="AppBundle:Horoscope", options={
 *    "mapping": {"slugHoroscope": "slug"},
 *    "repository_method" = "findByComplexSlug",
 *    "map_method_signature" = true
 * })
 */
public function homePageAction(Horoscope $horoscope, $slugHoroscope, ZodiacService $zodiacService, Request $request)
{
    $form = $this->createForm(DateType::class);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $date = $form->get('date')->getData();

        $parameters = [
            'slugHoroscope' => $slugHoroscope,
            'slugZodiac'    => $zodiacService->getSignByDate($date)->getSlug(),
        ];

        return $this->redirectToRoute('horoscope_detail', $parameters);
    }

    return $this->render(':horoscope:acc-horoscope.html.twig', [
        'horoscope' => $horoscope,
        'form'      => $form->createView()
    ]);
}

/**
 * @Route("/{slugHoroscope}/{slugZodiac}.{_format}", name="horoscope_detail", defaults={"_format": "html"})
 *
 * @ParamConverter("horoscopeSigne", class="AppBundle:HoroscopeSigne", options={
 *    "mapping": {"slugHoroscope": "slugHoroscope", "slugZodiac": "slugZodiac"},
 *    "repository_method" = "findByComplexSlug",
 *    "map_method_signature" = true
 * })
 */
public function detailAction(HoroscopeSigne $horoscopeSigne, $slugHoroscope, Request $request)
{
    $horoscope = $horoscopeSigne->getHoroscope();

    $form = $this->createForm(DateType::class, null, [
        'action' => $this->generateUrl('horoscope_homepage', ['slugHoroscope' => $slugHoroscope])
    ]);

    $template = $horoscopeSigne
        ->getHoroscope()
        ->getHoroscopeModele()
        ->getTemplatePath();

    return $this->render('horoscope/'.$template, [
        'horoscope'      => $horoscope,
        'horoscopeSigne' => $horoscopeSigne,
        'form'           => $form->createView(),
    ]);
}