如何在Symfony Controller中的不同功能中使用相同的代码?

时间:2019-03-04 11:43:11

标签: function symfony controller

在我的控制器中,我正在使用几种功能。在此功能中,我使用了类似的代码。

所以我想知道是否有可能将此代码外包,而不必重复编写。如果可能的话,最好的方法是什么?

class PagesController extends AbstractController
{
  /**
  * @Route("/documents/{slug}", name="documents", methods={"GET","POST"})
  */

  public function documents($slug,  Request $request)
  {
     $page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
     $entityManager = $this->getDoctrine()->getManager();
    $cmf = $entityManager->getMetadataFactory();
    $classes = $cmf->getMetadataFor($relation_name);
    $fieldMappings = $classes->fieldMappings;
    $associationMappings = $classes->associationMappings;
    $fields = (object)array_merge((array)$fieldMappings, (array)$associationMappings);
  }


  /**
  * @Route("/blog/{slug}", name="single", methods={"GET","POST"})
  */


  public function blog($slug,  Request $request)
  {
     $page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
     $entityManager = $this->getDoctrine()->getManager();
    $cmf = $entityManager->getMetadataFactory();
    $classes = $cmf->getMetadataFor($relation_name);
    $fieldMappings = $classes->fieldMappings;
    $associationMappings = $classes->associationMappings;
    $fields = (object)array_merge((array)$fieldMappings, (array)$associationMappings);
  }

  /**
  * @Route("/contact/{slug}", name="contact", methods={"POST", "GET"})
  */

  public function contact($slug,  Request $request)
  {
     $page = $this->getDoctrine()->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
     $entityManager = $this->getDoctrine()->getManager();
    $cmf = $entityManager->getMetadataFactory();
    $classes = $cmf->getMetadataFor($relation_name);
    $fieldMappings = $classes->fieldMappings;
    $associationMappings = $classes->associationMappings;
    $fields = (object)array_merge((array)$fieldMappings, (array)$associationMappings);
  }
}

3 个答案:

答案 0 :(得分:2)

您可以使用私有方法并调用它,但是在您的情况下,可以在参数中直接使用Page typehint:

/**
 * @Route("/contact/{slug}", name="contact", methods={"POST", "GET"})
 */
 public function contact(Page $slug, Request $request)

答案 1 :(得分:1)

此处的关键字是服务。将业务逻辑移至其他类,然后使用autowiring将其自动注入到控制器中。这是Symfony Best Practice

  

Symfony遵循“瘦控制器和胖模型”的哲学。   这意味着控制器应仅容纳   协调应用程序不同部分所需的粘合代码。

您应该阅读这些最佳做法!

您可以在控制器类和特定操作中注入服务:

class PagesController extends AbstractController
{
  public function __construct(Rot13Transformer $transformer)
  {
    $this->transformer = $transformer;
  }

  /**
  * @Route("/documents/{slug}", name="documents", methods={"GET","POST"})
  */

  public function documents($slug,  Request $request, PagesRepository $repo)
  {
     $page = $repo->findOneBy(['slug'=>$slug]);

     $foo = $repo->doSomethingDifferentWithEntities($page)

     $bar = $this->transformer->transform($foo);
  }
}

答案 2 :(得分:1)

@Jarla除了@Stephan Vierkant答案以外,您还可以使用@ParamConverter annotation 就您而言,它将是:

int[] arr = { 1, 2, 3, 4, 5, 6 };
//int[] arr = { 5, 6, 9 }; 
//string[] arr = { "A", "B", "C" }; 

var result = Enumerable.Range(1, arr.Length).Select(i => arr[i % arr.Length]).ToArray();

foreach (var item in result)
{
    Console.WriteLine(item);
}