In al mijn vorige projecten in Symfony 2 gebruikte ik third-party bundles om niet constant het wiel opnieuw uit te vinden. In mijn laatste projecten (welke trouwens allemaal hobbymatig zijn) ben ik meer van het stock (om de vendors directory niet onnodig vol te stoppen). Call me a sucker, I don't care. Beetje last van perfectionisme.
In het weekend geprobeerd een nieuwe AccountController te bouwen in Symfony, wat aardig lukt, alleen vroeg ik mij af of er iemand is hier die een beetje kennis heeft van Symfony die mij kan vertellen of ik wel de juiste methodes/best-practices gebruik. Ik heb namelijk het idee dat ik ergens de plank compleet mis sla.
Alvast bedankt voor het bekijken, en sorry vanwege de grote lap code.
In het weekend geprobeerd een nieuwe AccountController te bouwen in Symfony, wat aardig lukt, alleen vroeg ik mij af of er iemand is hier die een beetje kennis heeft van Symfony die mij kan vertellen of ik wel de juiste methodes/best-practices gebruik. Ik heb namelijk het idee dat ik ergens de plank compleet mis sla.
Alvast bedankt voor het bekijken, en sorry vanwege de grote lap code.
PHP: AccountController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
| <?php // src/AppBundle/Controller/AccountController.php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use AppBundle\Entity\Account; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; /** * @Route("/account") */ class AccountController extends Controller { /** * @param Request $request * @return array * * @Route("/login", name="account_login") * @Method({"GET", "POST"}) * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')") * @Template("AppBundle:account:login.html.twig") */ public function loginAction(Request $request) { $helper = $this->get('security.authentication_utils'); $form = $this->createFormBuilder() ->setAction($this->generateUrl('account_login_check')) ->setMethod('POST') ->add('username', 'text', array( 'data' => $helper->getLastUsername() )) ->add('password', 'password') ->add('submit', 'submit', array( 'label' => 'Sign in' )) ->getForm(); return array( 'form' => $form->createView(), 'error' => $helper->getLastAuthenticationError() ); } /** * @param Request $request * @throws \Exception * * @Route("/login_check", name="account_login_check") * @Method({"POST"}) * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')") */ public function loginCheckAction(Request $request) { throw new \Exception(); } /** * @param Request $request * @throws \Exception * * @Route("/logout", name="account_logout") * @Method({"GET"}) * @Security("is_granted('IS_AUTHENTICATED_FULLY')") */ public function logoutAction(Request $request) { throw new \Exception(); } /** * @param Request $request * @return array|\Symfony\Component\HttpFoundation\RedirectResponse * * @Route("/register", name="account_register") * @Method({"GET", "POST"}) * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')") * @Template("AppBundle:account:register.html.twig") */ public function registerAction(Request $request) { $form = $this->createFormBuilder($account = new Account()) ->setAction($this->generateUrl('account_register')) ->setMethod('POST') ->add('username', 'text') ->add('email', 'email') ->add('plainPassword', 'repeated', array( 'type' => 'password', 'invalid_message' => 'The password fields must match.' )) ->add('submit', 'submit', array( 'label' => 'Sign up' )) ->getForm(); $form->handleRequest($request); if ($form->isValid()) { $account = $form->getData(); $encoder = $this->container->get('security.encoder_factory')->getEncoder($account); $account->setConfirmationToken(md5($_SERVER['HTTP_USER_AGENT'] . time())); $account->setSalt(bin2hex(mcrypt_create_iv(64, MCRYPT_DEV_URANDOM))); $account->setPassword($encoder->encodePassword($account->getPlainPassword(),$account->getSalt())); $em = $this->getDoctrine()->getManager(); $em->persist($account); $em->flush(); $mailer = $this->get('mailer'); $mailer->send($mailer->createMessage() ->setSubject('Activate your account') ->setFrom(array('from@domain' => 'From us')) ->setTo(array($account->getEmail() => $account->getUsername())) ->setBody($this->renderView('mails/register.html.twig', array( 'username' => $account->getUsername(), 'password' => $account->getPlainPassword(), 'code' => $account->getConfirmationToken() )), 'text/html') ); $this->addFlash('success', 'Your account has been created but requires activation. Please see our activation e-mail for more information.'); return $this->redirect( /** * ("/login", name="account_login") */ $this->generateUrl('account_login') ); } return array( 'form' => $form->createView() ); } /** * @param Request $request * @param $code * @return \Symfony\Component\HttpFoundation\RedirectResponse * * @Route("/activate/{code}", name="account_activate") * @Method({"GET"}) * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')") */ public function activateAction(Request $request, $code) { $account = $this->getDoctrine()->getRepository('AppBundle:Account')->loadUserByActivationCode($code); $em = $this->getDoctrine()->getManager(); $account->setIsActive(true); $account->setConfirmationToken(null); $em->persist($account); $em->flush(); $this->addFlash('success', 'Your account has been activated.'); return $this->redirect( /** * ("/login", name="account_login") */ $this->generateUrl('account_login') ); } /** * @param Request $request * @return ? * * @Route("/request", name="account_password_request") * @Method({"GET", "POST"}) * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')") * @Template("AppBundle:account:forgot.html.twig") */ public function forgotAction(Request $request) { // } /** * @param Request $request * @return ? * * @Route("/reset/{confirmationToken}", name="account_password_reset") * @Method({"GET"}) * @Security("is_granted('IS_AUTHENTICATED_ANONYMOUSLY')") */ public function newForgotAction(Request $request) { // } } |
.Gertjan.: Ik ben een zelfstandige alcoholist, dus ik bepaal zelf wel wanneer ik aan het bier ga!