|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Tag; |
|
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class AdminController |
|
16
|
|
|
* @package AppBundle\Controller\Admin |
|
17
|
|
|
* @Route("/admin") |
|
18
|
|
|
*/ |
|
19
|
|
|
class TagController extends Controller |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @Method("GET") |
|
23
|
|
|
* @Route("/tags", name="tagsAdmin") |
|
24
|
|
|
* @Template("AppBundle:admin:tags.html.twig") |
|
25
|
|
|
* |
|
26
|
|
|
* @return Response |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function roleAction() |
|
29
|
|
|
{ |
|
30
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
|
31
|
1 |
|
$tags = $em->getRepository('AppBundle:Tag') |
|
32
|
1 |
|
->findAll(); |
|
33
|
|
|
|
|
34
|
|
|
return [ |
|
35
|
1 |
|
'tags' => $tags, |
|
36
|
1 |
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Request $request |
|
41
|
|
|
* @Route("/tag/new", name="tagNew") |
|
42
|
|
|
* @Method({"GET", "POST"}) |
|
43
|
|
|
* @Template("AppBundle:admin/form:tag.html.twig") |
|
44
|
|
|
* |
|
45
|
|
|
* @return Response |
|
46
|
|
|
*/ |
|
47
|
|
|
public function editRoleAction(Request $request) |
|
48
|
|
|
{ |
|
49
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
50
|
|
|
|
|
51
|
|
|
$tag = new Tag(); |
|
52
|
|
|
$title = 'Create new tag'; |
|
53
|
|
|
|
|
54
|
|
|
$form = $this->createFormBuilder($tag) |
|
|
|
|
|
|
55
|
|
|
->setAction($this->generateUrl('tagNew')) |
|
56
|
|
|
->setMethod('POST') |
|
57
|
|
|
->add('name', TextType::class, array( |
|
58
|
|
|
'attr' => array('placeholder' => '* Tag name'), |
|
59
|
|
|
) |
|
60
|
|
|
) |
|
61
|
|
|
->add('save', SubmitType::class, array('label' => 'Save')) |
|
62
|
|
|
->getForm(); |
|
63
|
|
|
|
|
64
|
|
|
if ($request->getMethod() == 'POST') { |
|
65
|
|
|
$form->handleRequest($request); |
|
66
|
|
|
if ($form->isValid()) { |
|
67
|
|
|
$em->persist($tag); |
|
68
|
|
|
$em->flush(); |
|
69
|
|
|
|
|
70
|
|
|
return $this->redirectToRoute('tagsAdmin'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return [ |
|
75
|
|
|
'title' => $title, |
|
76
|
|
|
'form' => $form->createView(), |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
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 sub-classes 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 parent class: