@@ -139,7 +139,10 @@ Finally, you need to update the code of the controller that handles the form::
139
139
$fileName = md5(uniqid()).'.'.$file->guessExtension();
140
140
141
141
// Move the file to the directory where brochures are stored
142
- $file->move($this->container->getParameter('brochures_directory'), $fileName);
142
+ $file->move(
143
+ $this->container->getParameter('brochures_directory'),
144
+ $fileName
145
+ );
143
146
144
147
// Update the 'brochure' property to store the PDF file name
145
148
// instead of its contents
@@ -199,4 +202,95 @@ You can use the following code to link to the PDF brochure of an product:
199
202
View brochure (PDF)
200
203
</a>
201
204
205
+ Creating an Uploader Service
206
+ ----------------------------
207
+
208
+ To avoid logic in controllers, making them big, you can extract the upload
209
+ logic to a seperate service::
210
+
211
+ // src/AppBundle/FileUploader.php
212
+ namespace AppBundle\FileUploader;
213
+
214
+ use Symfony\Component\HttpFoundation\File\UploadedFile;
215
+
216
+ class FileUploader
217
+ {
218
+ private $targetDir;
219
+
220
+ public function __construct($targetDir)
221
+ {
222
+ $this->targetDir = $targetDir;
223
+ }
224
+
225
+ public function upload(UploadedFile $file)
226
+ {
227
+ $fileName = md5(uniqid()).'.'.$file->guessExtension();
228
+
229
+ $file->move($this->targetDir, $fileName);
230
+
231
+ return $fileName;
232
+ }
233
+ }
234
+
235
+ This class can be registered as a service in the service container:
236
+
237
+ .. configuration-block ::
238
+
239
+ .. code-block :: yaml
240
+
241
+ # app/config/services.yml
242
+ services :
243
+ # ...
244
+ app.brochure_uploader :
245
+ class : AppBundle\FileUploader
246
+ arguments : ['%brochures_directory%']
247
+
248
+ .. code-block :: xml
249
+
250
+ <!-- app/config/config.xml -->
251
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
252
+ <container xmlns =" http://symfony.com/schema/dic/services"
253
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
254
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
255
+ http://symfony.com/schema/dic/services/services-1.0.xsd"
256
+ >
257
+ <!-- ... -->
258
+
259
+ <service id =" app.brochure_uploader" class =" AppBundle\FileUploader" >
260
+ <argument >%brochures_directory%</argument >
261
+ </service >
262
+ </container >
263
+
264
+ .. code-block :: php
265
+
266
+ // app/config/services.php
267
+ use Symfony\Component\DependencyInjection\Definition;
268
+
269
+ // ...
270
+ $container->setDefinition('app.brochure_uploader', new Definition(
271
+ 'AppBundle\FileUploader',
272
+ array('%brochures_directory%')
273
+ ));
274
+
275
+ Now you're ready to use this service in the controller::
276
+
277
+ // src/AppBundle/Controller/ProductController.php
278
+
279
+ // ...
280
+ public function newAction(Request $request)
281
+ {
282
+ // ...
283
+
284
+ if ($form->isValid()) {
285
+ $file = $product->getBrochure();
286
+ $fileName = $this->get('app.brochure_uploader')->upload($file);
287
+
288
+ $product->setBrochure($fileName);
289
+
290
+ // ...
291
+ }
292
+
293
+ // ...
294
+ }
295
+
202
296
.. _`VichUploaderBundle` : https://github.com/dustin10/VichUploaderBundle
0 commit comments