djuro95

Service

Feb 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. Service
  2. + +
  3. kreiramo CarBundle/Service/DataChecker.php
  4.  
  5. public function checkCar(Car $car)
  6. {
  7. return "Car " . $car->getModel() . " checked";
  8. }
  9.  
  10. *************************
  11.  
  12. CarBundle/Resouce/config/services.yml
  13.  
  14. services:
  15. car.data_checker:
  16. class: CarBundle\Service\DataChecker
  17.  
  18. ******************************
  19. CarController
  20.  
  21. /**
  22. * @param $id
  23. * Promote a car
  24. * @Route("/promote/{id}", name="car_promote")
  25. */
  26. public function promoteAction($id)
  27. {
  28. return $this->redirectToRoute("car_index");
  29. }
  30.  
  31. ***********************
  32.  
  33. View index.html.twig
  34.  
  35. <a href="{{ path('car_promote', {'id' : car.id}) }}">promote</a>
  36.  
  37.  
  38. ----------------------------
  39.  
  40. CarContoler
  41.  
  42. /**
  43. * @param $id
  44. * Promote a car
  45. * @Route("/promote/{id}", name="car_promote")
  46. */
  47. public function promoteAction($id)
  48. {
  49. $dataChecker = $this->get('car.data_checker');
  50. $em = $this->getDoctrine()->getEntityManager();
  51.  
  52. $car = $em->getRepository('CarBundle:Car')->find($id);
  53.  
  54. $result = $dataChcker->checkCar($car);
  55.  
  56. $this->addFlash('success', $result);
  57.  
  58. return $this->redirectToRoute("car_index");
  59. }
  60.  
  61. ****************************
  62. base.html.twig
  63.  
  64. {% for type, flashes in app.session.flashbag.all %}
  65. {% for flash in flashes %}
  66. <div class="alert alert-{{ type }} fade in">
  67. {{ flash }}
  68. </div>
  69. {% endfor %}
  70. {% endfor %}
  71.  
  72. -------------------------------------
  73.  
  74. class DataChecker
  75.  
  76. /** @var EntityManager
  77. protected $entityManager;
  78.  
  79. /**
  80. *DataChecker constructor
  81. *
  82. * @param EntityManger $entityManger
  83. */
  84. public function __construction($entityManager)
  85. {
  86. $this->entityManager = $entityManger;
  87. }
  88.  
  89.  
  90. ******************
  91.  
  92. CarBundle/Resouce/config/services.yml
  93.  
  94. services:
  95. car.data_checker:
  96. class: CarBundle\Service\DataChecker
  97. arguments: ["@doctrine.orm.entity_manager"]
Add Comment
Please, Sign In to add comment