17.
Override Controllers
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.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
20 Comments
Hi,
It's so weird for me that just when I add "EasyAdmin" folder at my bundle, I get the message that:
No route found for "GET /admin/"
I've just check any possible solutions without any success :(
Any idea?
Hey Mohammad
Sorry for the late reply! Do you still have this problem?
By a chance did you do any other modification before adding that folder? I believe you are missing a route definition.
Or maybe, EasyAdminBundle people have made some changes since this tutorial was published.
If that's not the case, lets us know :)
Cheers!
Btw: In Symfony 4 with EasyAdmin Bundle with Flex, you need to extend this class: EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController instead of: JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController
This worked for me :)
Hey @Adam
Thanks for notifying us about that. I just did some digging and found that that change was made in this release: https://github.com/EasyCorp...
So, yes, you have to update all your references if you were using our code and then you decided to update EasyAdminBundle.
Cheers!
Hi
Are you planning to update the tutorial on EasyAdmin with SF4 + connection with Guard ?
best regards,
Gregory
Hey Greg,
Btw, we do cover Guard in our Symfony 4 tutorials, for example in this one about Security: https://symfonycasts.com/sc... - have you watched it?
> EasyAdmin with SF4 + connection with Guard
Also, what do you mean about this? EasyAdmin is just a tool to generate admin interface for your Symfony aplpications. But Guard is all about authentication. So, they are not tight to each other and are separate things. And we do have separate screencasts about them. What exactly problem do you have using Guard and EasyAdmin?
Cheers!
Hi Victor
I found my way ;)
I had to implode my own method to encrypt the password when I create a user.
I didn't think before I posted the comment ;)
EasyAdmin is so powerful when you understood how to override the view and method.
BTW with the last version of EasyAdmin you can't override preUpdateEntity, I used to override updateEntity and persistEntity
Thanks again for your wonderful works to all the Symfonycast team.
Happy Christmas and Happy new Year
Hey Greg,
Ah, I see :) Glad you got it working!
And good to know about "preUpdateEntity()", thanks for this tip!
Cheers!
Hey Gregory
That's not in our milestone but thanks for letting us know what kind of tutorials you would like to see in the future. We *do* consider your opinion when deciding what tutorial is going to be next
BTW, do you have any particular problem installing EasyAdmin on SF4?
Cheers!
Hi Diego,
Thank you for your answer.
I have no problem with EasyAdmin, I just need to think more before I talked ;)
Cheers.
Hi,
I've got an image property in my product form. I just took it out of the workflow, and want to save the images in my other entity ProductImages. Now I want to save them, but how do I get the images in my controller?
I've tried this:
But that does not return the images. It just dumps the entity.
BR,
Dennis
Yo Dennis!
Hmm. Sorry for our late reply :). Can you show a little bit more code? If I understand it correctly, your editing a "Product", and you've added a custom "image" property to that form (but there is no image property on your Product entity - that is ok, I'm just clarifying). Now, you want to read the submitting image from the form and use it to actually save data on a different entity - ProductImage. Is that accurate?
So, you really need access to the form object in your controller, because it will contain the "image" data. And yea, that's a bit tricky - a bit too tricky in my opinion. Here is how I think it will need to work:
1) In your custom controller, override the
createEntityFormmethod. Just call the parent() method, but store the form on a new property:2) In your preUpdateEntity, you can now access the form, which should allow you to access the image field:
If the user uploaded a file, this should be an UploadedFile object.
Let me know if this helps!
Hi Ryan,
I figured it out already. I do have an image property in my product class. Because one product can have multiple images. So I do need it to be uploaded in the form.
So just to clarify:
I do have a product entity, with an image attribute. That image attribute is linked to my productImages entity. I save the images (URL's) in the database, and upload the images with a service I created. FileHandlerService. Now when I upload the productImages, it will return an array of URL's. And I pass that with this entity:
Do you think this is bad practice?
Hope to hear from you, and thanks for your time, patience and help.
Best regards,
Dennis
Hey Dennis!
Sorry for my late reply - your message got lost in SPAM for some reason. Tisk tisk Disqus!
I still don't fully understand your setup - your have an "image" property on Product, but each Product also has many images? So, the image property is just a non-persisted property that you added to Product so that you form would work? Is that correct?
But mostly, I don't see any issues with your setup :). Unrelated to your original question, I would actually register that FileHandler class as a service, remove the first constructor argument from it and pass the productImagesDir to the constructor. So it would look like:
Then, when you call the method, pass in the entity only:
It's generally a good idea to pass services & static config to the constructor. Then pass any entities or "per-call configuration" to the method itself. This will create really-nicely and re-usable service classes.
Cheers!
Hi Ryan,
I think I ask too many questions!
I'm sorry if I did not make myself clear enough. English is not my first language (I'm Dutch).
No that's not correct. I have 2 entities. Product and ProductImages. The ProductImages saves the URL to the database. The FileHandler *is* a service actually.
I should inject the FileHandler in the constructor of the productController, and then I should inject the EM in the FileHandler constructor, together with the upload directory, right? And pass the entity as a parameter of the saveImages function.
My FileHandler->saveImages() returns an array of objects, to save it to the DB (or atleast not get an error).
Thanks for your time and patience.
Best regards,
Dennis
Hey Dennis!
I think you can do all that work in your FileHandler (validate and save to the DB), by injecting EntityManager and the upload directory. Then, in your controller inject that service and just use it, passing through the entity
Cheers!
Hi :-)
I'm trying to get a smooth admin interface similar as if when two entities are associated by a many-to-many relationship. I need the join table to define additional information like rank. I don't want to display the jointable entity on the backend, it should be writeable in the edit-menu of at least one entity. How can I make an edit form access the values of the jointable?
Hey @Riccardo Previti
Usually when you have a many-to-many relationship you don't need an associated entity, but when you need custom fields for that relation (let's say, createdAt), then you will have to create a new entity that will hold those values and a reference to the related entities. In this case you will end up with 3 classes with a one-to-many relationship.
You can find good examples here: http://codemonkeys.be/2013/...
Also you may find helpful this tutorial to start working with associations: http://knpuniversity.com/sc...
Have a nice day!
Thank you for your time. You're exactly talking about what I want. I already did the tutorial and the relation is already set up. Maybe I did not express myself well, but I am only concerned about how to make it look like a many to many in easyadmin's backend entity. For now, it only tries to list joinTable entities. Those should not be choosen, but generated when choosing the to-be-related entity. Example:
Many Questions can have many Options.
In my backend I want to create questions and assign options to it or create options and see which questions they belong to. Can't find any documentation anywhere on this
Oh I got you now, I'm not an expert using EasyAdminBundle, but I think you will have to put in practice almost everything you have learned in this tutorial, like override a layout, a form, controller action, etc.
After doing it, you will be able to do anything you want in EasyAdminBundle :)
Cheers!
"Houston: no signs of life"
Start the conversation!