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!
09.
URL to Public Assets
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
}
}
24 Comments
Hi ! I have a lot of entity with image that use the same url as path :
This path is in a
.envfile. How can I retrieve it in my entity ?Hey Paul,
Yeah, that's a problem... Well, you may fetch env vars from
$_ENVor withgetenv()from the entities - that's not a good practice I think, but it may work this way. Also, you may want to use PHP traits or inheritance to avoid code duplications.As an alternative way, you can return only the unique (not full) path from the entities, and in the places where you use that path - prefix it with the dynamic env var value. Or you can extract that into a service that will merge the dynamic env var with the unique entity path and creates the full path - you can path the whole entity object into it if needed.
I hope that helps.
Cheers!
Hello ! :-)
Why I need to use '/uploads/' . $path when you just use 'uploads/' . $path? Why do I need that starting slash to make it work ?
Hey Loqman
That's because we are passing our image path to the custom Twig function we've just created
uploaded_asset(). In the next chapter you'll learn more about how it works internallyCheers!
Thanks for your reply, literally the entire next video is the answer to my question :D
NP! Happy to help :)
Lets say I have an application that manages the students in a college starting from admission all the way to the time they graduate. Such an application might require that the images (Profile Photo, Id documents, credentials etc) of each student stored in a directory that is named after the student or more appropriately the registration number of the student vsay 2021RC0004 . Is there a way to create a directory on the file system on the fly (by passing the student registration number in the POST payload) and move the file to this newly created directory e.g. public/uploads/2021RC0004? The entity can return the public path thst doesn't include the student registration number public/uploads/ the UI can concatenate the student registration number to obtain the complete directory path.
Hey sridharpandu
Yes, that's possible, you could leverage the Filesystem Symfony component to do that task. It's a nice abstraction for working the filesystem
https://symfony.com/doc/cur...
Cheers!
Hi, I've got an issue implementing that with webpack encore, I make a dump on ($this->container->get(UploaderHelper::class)->getPublicPath($path) and I saw the result is :
"uploads/provider/avatar/homme-60798628bf9b3.jpg"So I added "build" on the beginning of the path in the getPublicPath method :
return '/build/uploads/'.$pathBut it doesn't work because the unique id of the file is missing, my file on my server looks like that :
homme-60798628bf9b3.60aa3ab6.jpgHow can I fix that ? Thank you.
Hey YinYang,
Use the asset() Twig function - it will automatically add that hash at the end of file name for you.
Cheers!
Hi SymfonyCasts team! Thanks for this tutorial! However, I have a question (I haven't watched the sequel yet so it may be unnecessary?).
Why not just use the VichUploader bundle? It is so useful, easy to use, it would have saved a lot of time on a lot of aspects.
Maybe you didn't highlight it to show us how everything works, rather than going the easy way? Which would be totally logical. But talking about this bundle could be nice too! (Again I'm only at this chapter so maybe you'll talk about it later!)
Thank you for everything !
Hey Kiuega!
Yes, very fair question! I would say it's 2 major things:
1) Yes, I do like showing HOW things work... though that is not by itself a good enough reason to not use a library in a tutorial.
2) VichUploaderBundle - which I used to *love* - just feels a bit *too* magic to me. It is probably something that would work really well for most people, but I find that when you need to start doing more custom stuff, it's really unclear how to do extend. I prefer having more control.
That being said, in a perfect world, we'd probably have something "in between". And if you do need to do some uploading, giving VichUploaderBundle an hour to test out is a good idea in case it works really nicely for you.
Cheers!
Hi team,
Symfony is not happy about this line ->get(MarkdownHelper::class)->parse($value); in processMarkdown()
when i click on an article to show it, i got this error:
<blockquote>Argument 1 passed to App\Service\MarkdownHelper::parse() must be of the type string, null given, called in C:\xampp\htdocs\SymfonyCasts\Symfony4\AllaboutUplFiles\01_start\src\Twig\AppExtension.php on line 47
</blockquote>
When i comment it, it works, but without the makrdown text
AppExtension class
I appreciate your help !
Hey Raed
Losks like your
$valueis null somehow, check your template, how do you use this filter, and which value you pass to itCheers!
Hey ``sadikoff ,
Thanks for your reply !
Losks like your $value is null somehow, mmm, i don't think so because when i click to show an article with no photo
it gets rendered,
Here is show.html.twig code:
and MarkdownHelper.php class
Hey hey
You know...mmm.. my answer will not change, the only reason I see is
nullvalue insidearticle.contentvariable, so you can check it in Twig by using{{ dump() }}or insideAppExtensionusingdd()ordumpCheers!
Hey sadikoff ,
I appreciate your help ! By just re-loading the fixtures again , it works !
Hi team !
Where the getFunctions() name in twig extension come from ?
I tried to rename it to something else but i got an error as : Unknown "uploaded_asset" function.
public function getFunctions(): array
{
return [
new TwigFunction('uploaded_asset', [$this, 'getUploadedAssetPath'])
];
}
Thanks for your awesome courses.
Hey Raed.
That getFunctions() comes from the class you extends. So, it's required to have exactly that name so that Twig could register functions in your project. Otherwise, it won't be able to register the functions you listed there, e.g. uploaded_asset in your case. In other words, that method name should not be changed. But you can rename that "uploaded_asset" function name or associated "getUploadedAssetPath()" if needed. But if you change its name - make sure you change it in the whole project.
Cheers!
Hi victor,
Thanks a lot that cleared my doubt.
Hello, thank you for this great course.
I followed everything well and everything works so far.
I did not follow the other courses so I did not have "Twig/AppExtension.php", I created this class and the function {{uploaded_asset ()}} works in twig but does not return the $path ( 'uploads/').
Need to add a configuration somewhere?
Thank you
Hi ojtouch
Have you downloaded course code, like it was mentioned in First chapter? It doesn't return path because it's rely on another service, which probably not fully configured. It's hard to say something without seeing code :)
Cheers!
Hi Ryan, I think that info about ServiceSubscriber resides in https://symfonycasts.com/sc... not in fundamentals. Cheers :)
Ah, you're right! Thanks for the correction - we'll add a note!
"Houston: no signs of life"
Start the conversation!