2017-08-08 170 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

是的,再次感謝,我在實習中,所以我會看到主開發人員對這種方法的看法,但至少我不會再寫同樣的東西兩次:) –