2017-08-08 114 views
1

我与Symfony的3个工作,我有一个“优化”的问题:一种形式

  • 我有一种形式
  • 该形式在一个控制器
  • 使用
  • 但这种形式在
  • 两页使用

是否有可能创建视图+处理用户的anwser这种形式的一次为两页?

现在我看到的唯一的解决办法是复制我的代码两次,每次页面(=两个动作在我的控制器)

有什么建议?

** 
* @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'); 
    } 
} 
+0

你可以发布你的控制器代码吗?我们始终使用相同的控制器方法来处理多个页面和操作。 – ASOlivieri

+0

@ASOlivieri好吧,现在我没有在我的控制器中处理表单,我想在之前考虑一个更好的解决方案!如果我发现如何发布我的代码,我会尝试(我是新来的,对不起) –

回答

0

最终溶液由主开发建立是建立一个服务来处理的形式为“溶液”(一个星座),并在控制器执行以下操作:

/** 
* @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(), 
    ]); 
} 
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); 
    } 

} 
+0

好的谢谢!我会用私有方法尝试第二个选项:)我只需要使用Symfony请求对象作为我的函数的参数,并返回窗体为我的视图,但我认为它应该工作:) –

+0

是的,你必须通过在参数中返回某些东西,看起来像你的formtype中的日期,但它至少重用了代码。 – ASOlivieri

+0

是的,再次感谢,我在实习中,所以我会看到主开发人员对这种方法的看法,但至少我不会再写同样的东西两次:) –