|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: kate |
|
5
|
|
|
* Date: 17.02.16 |
|
6
|
|
|
* Time: 9:30 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace AppBundle\Controller; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use AppBundle\Entity\Comment; |
|
13
|
|
|
use AppBundle\Entity\Estate; |
|
14
|
|
|
use AppBundle\Form\CommentType; |
|
15
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
16
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class SiteController extends Controller |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @Route("/", name="homepage") |
|
27
|
|
|
*/ |
|
28
|
|
|
public function indexAction(Request $request) |
|
29
|
|
|
{ |
|
30
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
31
|
|
|
$estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles(); |
|
32
|
|
|
return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @Route("/menu", name="menu") |
|
37
|
|
|
*/ |
|
38
|
|
|
public function menuAction(Request $request) |
|
39
|
|
|
{ |
|
40
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
41
|
|
|
$categoryEntity = $em->getRepository('AppBundle\Entity\Category'); |
|
42
|
|
|
$categories = $categoryEntity->childrenHierarchy(); |
|
43
|
|
|
return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @Route("/show_category/{slug}", name="show_category") |
|
48
|
|
|
*/ |
|
49
|
|
|
public function showCategoryAction(Request $request, $slug) |
|
50
|
|
|
{ |
|
51
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
52
|
|
|
$estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($slug); |
|
53
|
|
|
|
|
54
|
|
|
return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @Route("/show_estate/{slug}", name="show_estate") |
|
59
|
|
|
*/ |
|
60
|
|
|
public function showEstateAction(Request $request, $slug) |
|
61
|
|
|
{ |
|
62
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
63
|
|
|
$estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug); |
|
64
|
|
|
// comment form |
|
65
|
|
|
$comment = new Comment(); |
|
66
|
|
|
$commentForm = $this->createForm(CommentType::class, $comment, [ |
|
67
|
|
|
'method' => 'POST', |
|
68
|
|
|
]) |
|
69
|
|
|
->add('addComment', SubmitType::class, ['label' => 'Save', |
|
70
|
|
|
'attr' => ['class' => 'btn btn-primary'] |
|
71
|
|
|
]); |
|
72
|
|
|
if ('POST' === $request->getMethod()) { |
|
73
|
|
|
$commentForm->handleRequest($request); |
|
74
|
|
|
if ($commentForm->get('addComment')->isClicked()) { |
|
|
|
|
|
|
75
|
|
|
if ($commentForm->isSubmitted() && $commentForm->isValid()) { |
|
76
|
|
|
$comment->setEstate($estate[0]); |
|
77
|
|
|
if ($this->getUser()->hasRole('ROLE_ADMIN')) { |
|
78
|
|
|
$comment->setEnabled(true); |
|
79
|
|
|
} else { |
|
80
|
|
|
$comment->setEnabled(false); |
|
81
|
|
|
} |
|
82
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
83
|
|
|
$em->persist($comment); |
|
84
|
|
|
$em->flush(); |
|
85
|
|
|
$this->get('session')->getFlashBag()->add('success', 'site.flush_comment'); |
|
86
|
|
|
return $this->redirectToRoute('show_estate', array('slug' => $estate[0]->getSlug())); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate[0], |
|
91
|
|
|
'commentForm' => $commentForm->createView())); |
|
92
|
|
|
} |
|
93
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: