https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/easyadminbundle-starting-with-wallpaper-uploads - full video tutorial series and code samples
We're going to want our Admin users to be able to upload new Wallpapers from our Admin Dashboard. To learn more, we're going to do things the hard way :)
Show More Show Less View Video Transcript
0:00
Hi, I'm Chris from codereviewvideos.com, and in this video, we're going to get started
0:08
allowing users to upload new wallpapers from our admin dashboard. In the previous video, I mentioned how up until a point, you can get away with using
0:17
like a MySQL GUI, whether it's PHP My Admin or SQL Pro or something like that, to entirely
0:22
avoid having to code up an admin panel. If it's just you working on the project, the same can sort of be said for having a file
0:29
upload process that you can do from the website itself like you might be okay with just uploading
0:34
files manually using ftp or rsync or something like that but almost inevitably sooner or later
0:40
you're going to want to do it from the back end and you're going to want the process to be a little bit nicer than that so given the fact that we're using easy admin bundle and that this is quite a
0:47
common problem it comes as no surprise that there is already an integration with an existing bundle
0:52
to help you with uploads so if you're that way inclined then do look at the vich uploader bundle
0:57
integration and the truth is that bundle and the integration is going to be a lot more robust than
1:02
what we're about to build but in my opinion it makes more sense to use that bundle if you know
1:07
the process that's going on underneath without using that bundle as inevitably if you add
1:11
something like this to your website it's going to become quite heavily used that means bugs are
1:16
going to get noticed and if those bugs fall on you to fix then that process is a lot easier if you
1:20
know how the code all works we know that we need to allow our users to enter a new wallpaper and so
1:26
So if we try and do that now you see that we expecting the user to provide the file name the slug the width and the height and the category so a lot of this information is stuff that we can figure out dynamically as we saw when we created our console command we can use built php functions to
1:40
determine the width and the height we could guess at the slug based on the file name and really we
1:44
don't want to capture the file name anyway we want to allow the user to click a button here and
1:49
upload the file from the front end so let's start by making some changes to enable this so back
1:55
inside our project I'm going to go in app config and then our config directory into our easy admin
2:00
bundle configuration and I'm going to create a new key at the same level as list which has the name
2:07
of form and then we need to specify which fields are on that form so in our case we'll have the
2:12
slug the width and the height and again that's command d on a mac control d on windows to
2:18
duplicate that line so if we save that refresh our form you can see that we've removed some of
2:23
those fields and if we enter any old junk in here try and save that off you can see that it won't
2:29
allow us to save because the file name is null and it's not allowed to be null but as we've just
2:33
seen we don't want to capture the file name directly we want to capture that from a file
2:38
an uploaded file so if we're capturing an uploaded file we need to add that as a concept onto our
2:43
entity so we need to jump into source at bundle entities and then into our wallpaper entity
2:48
and just at the top here immediately under category I'm going to add in a new one for
2:54
a private class property of file and that is going to be a string it going to be the file name and we also add in the ORM column the name of file a type of string The only other thing that I want to do at this stage is adding a getter and a setter
3:09
for file so I'm going to do that now adding in the new getter and setter just above the getter
3:14
and setter for file name. I just like to keep it structured in the same way that my class properties
3:18
are. With that change made to my wallpaper entity I'm going to generate a new doctrine migration
3:24
by using the doctrine migrations diff command. So let's go ahead and create a new migration
3:29
containing the change that we've just made to our wallpaper entity. And I'm just going to up arrow there and do a doctrine migrations migrate
3:37
Don't need to put in the full command there. And we can see that's gone through. That was clear off that
3:42
And then if we jump back into here, I can get rid of that. Pretty sure I can get rid of that for the moment
3:46
And under our form fields now, I'm going to use another one of these built-in property types from EasyAdminBundle
3:52
So our property is going to be that file. It's going to have the type of file
3:57
That's going to make sure that we get the right input on our generated form. And we'll just leave it at that for the moment
4:02
Refresh our form. And that looks good enough. If we needed to, we could come back in here and change the label
4:08
But as it is, it's already got the correct label for us. And at this point, it's really easy to feel confused about what's going to happen
4:15
So when we post in this form, it's going to contain an image file
4:19
It can contain any kind of file, but in this case it's going to be an image file
4:23
That image is going to get uploaded to our server. At the time that image is uploaded PHP will store it in a temporary location As the name implies the location it stored is only temporary We must move it to somewhere that we control In our case we want to put it into our web images directory
4:40
Because we'll be working with a file, we will be able to determine things like the width and the height and potentially even the slug
4:47
Taking guidance from the cookbook article as referenced in the write-up to this video
4:51
we're going to use a doctrine lifecycle event to hook in to the process just before the new
4:57
wallpaper entity is saved off to the database or persisted to add this information to our entity
5:02
and then save it. Likewise, we're going to do something very similar when we come to edit
5:07
an existing wallpaper. And because it's quite involved and a little bit fiddly
5:11
it makes sense at this point to introduce testing. And for our testing, we're going to use PHP spec
5:15
so I'm going to jump across to the command line do a composer require dash dash dev php spec php
5:22
spec I'm just going to let it do its thing and whilst I appreciate this is a beginner's series
5:27
it's really never too early to start testing definitely will make your code more robust
5:31
and in my opinion it'll make you a better developer too anyway we'll jump back into our
5:35
project composer jason's been updated with our php spec dependency and the associated change has
5:41
been written to composer lock there is one other thing that we need to do inside our composer jason
5:46
which is directly from the php spec documentation which i'll link to in the show notes we just need
5:51
to make sure that we've got the auto load settings in for psi zero and in fact i can just copy paste
5:56
those ones we should be good that's all we need to do for php spec to get started
#Skins, Themes & Wallpapers
#Photo & Image Sharing
