Chapters
40 Chapters
|
4:39:58
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
13.
File Uploads & Data Fixtures
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial is built on Symfony 4 but works great in Symfony 5!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.87.10
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.9.0
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.22
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.0.3
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.4
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.2.3
"symfony/console": "^4.0", // v4.2.3
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.2.3
"symfony/framework-bundle": "^4.0", // v4.2.3
"symfony/property-access": "4.2.*", // v4.2.3
"symfony/property-info": "4.2.*", // v4.2.3
"symfony/security-bundle": "^4.0", // v4.2.3
"symfony/serializer": "4.2.*", // v4.2.3
"symfony/twig-bundle": "^4.0", // v4.2.3
"symfony/validator": "^4.0", // v4.2.3
"symfony/web-server-bundle": "^4.0", // v4.2.3
"symfony/yaml": "^4.0", // v4.2.3
"twig/extensions": "^1.5" // v1.5.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.1.0
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.2.3
"symfony/dotenv": "^4.0", // v4.2.3
"symfony/maker-bundle": "^1.0", // v1.11.3
"symfony/monolog-bundle": "^3.0", // v3.3.1
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.2.3
"symfony/stopwatch": "4.2.*", // v4.2.3
"symfony/var-dumper": "^3.3|^4.0", // v4.2.3
"symfony/web-profiler-bundle": "4.2.*" // v4.2.3
}
}
11 Comments
Hello,
When I follow the steps, i got an error saying "The service "App\Service\UploaderHelper" has a dependency on a non-existent service "uploadsPath"." Can anyone help me ?
Hey Aurélien Dachier!
Hmmm. Do you have any config in your services.yaml file for UploaderHelper? And also, does your "bind" line look like this? https://symfonycasts.com/screencast/symfony-uploads/uploader-service#codeblock-9d72416900
Usually, this error would make me think that you have some config that makes it look like "uploadsPath" is a service - some config like:
Or perhaps:
Let me know what you see in your services.yaml file :).
Cheers!
Hi ! Thanks for your answer, here's what i have in my services.yaml file :
Did I make something wrong ?
Hey Aurélien Dachier!
Excellent - this is very useful! When you have the
@uploadsPathvalue in this file, it tells Symfony to look for a service whose id isuploadsPath, which is not what you're intending. My guess is that you actually want to pass in one of those two parameters. In that case, you would use something like$uploadsPath: '%app.path.product_images%'.Let me know if that helps :).
Cheers!
Hello,
When i learned for the first time how to uploading file, it was recommended by Fabien Potencier to create an entity for the file and manage the UploadeFile class in the entity with doctrine events, for example App\Entity\Image. Today, i always follow this paradigm for my apps and manage all the files as mapped relations with Doctrine, so the approach of this tutorial perturbs me. Do you consider is not a good practise ? if yes, why ?
Source (fifth item) : https://openclassrooms.com/...
Regards
Hey Jean M.!
I think it's a fine approach :). It's not the one I prefer - but there is not super important reason for that. The thing I don't like about the entity solution (though, again, it's not such a major detail) is that it requires you to have a property on your entity that exists *just* to temporarily hold the UploadedFile - it's not meant to be persisted. Using an unmapped field in a form removes the need for this property.
The other thing that I don't prefer with lifecycle callbacks for this, is that it requires all of your logic to be inside of your entity - but I would usually prefer to have this logic in a service. This also limits you to saving things to the local filesystem - you can't use something like Flysystem, because you can't access services from inside your entity.
So, I see the lifecycle events approach to file uploading as a simpler, easier way of doing things. But I personally prefer to split things up a bit more, just because I like to have the logic in a service. But it sounds like you have had a lot of luck with the simpler approach - which does not surprise me. So if you want to keep doing it, no complaints from me ;).
Cheers!
Ok, it was an old tutorial, an easier way for basic usage i think but services are better way for customization. No problem for changing my paradigm. So i continue the course.
Thanks!
5:50 Can't you just use $test argument of UploadedFile constructor? It seems to be not marked as internal. Personally I don't like messing up my code logic just for fixtures. :)
Hey TomaszGasior
That's a good point, I think you could use that flag argument if you want but I don't see the uploading logic being polluted with fixtures logic, all the pesky details of copying files live inside fixtures
Cheers!
A big THANKS for this chapter.
Hey Manoj,
Thank you for your feedback! We're really happy you like it and it's useful for you ;)
Cheers!
"Houston: no signs of life"
Start the conversation!