php - How to send a form on a distant API using symfony2 forms -
I created a bundle of all the webservices in my Simply 2 project which used to save my files in the cloud (eg That Amazon S3). Some of these webservices allow files to send files directly without using them:
& lt; Form action = "api.thecloud.com/specificId?reirectUrl=MyUrlToGetTheResponse" & gt; & Lt; Input type = "file" name = "file" /> & Lt; Input type = "submit" / & gt; & Lt; / Form & gt; The given redirectUrl perm allows me to get information from the upload and save it in my database.
My problem is that I want to make it transparent and adaptable to other bundles, such as with the standard Symphony form type. Is there a good way to do this?
The solution I found is a bit complicated and I think there is a cleaner way to do this:
I make a type of type that specifies the action URL:
public function buildForm (formbuilder interface $ builder, array $ option) {$ builder- & gt; Add ('file', 'file'); $ Builder- & gt; Add ('Other Postupload Info', 'Hidden'); $ Manufacturer- & gt; Set Action ($ -E-> Cloud Services-> GenerateUpload URL ('my_redirect_url')); } Public Function getName () {Return ''; } The action route is given in another way (php or twig function) and is saved in one session.
In the action of my_redirect_url, I receive notification from the website and redirect to do the work saved in the session:
public function myRedirectUrlAction () {$ request = $ This- & gt; GetRequest (); // Reserve a form with received information $ rsrc = New CloudUploadRessource ($ request-> query- & gt; find ('ID'), get $ request- & gt; query- & gt; (' Url ')); $ form = $ this- & gt; createForm (new CloudUploadType (), $ rsrc); // converts it into an array and adds it to the request as if it is $ formArray = $ this- & gt; Transform FormErray ($ form-> createView ()) has been posted; $ Request- & gt; Request-> Add ($ formArray); // Redirection on client action URL with request // so that $ form-> Can bind ($ request); $ Form & gt; If (); ... $ clientActionRoute = $ request- & gt; GetSession () - & gt; Obtain ('clientActionRoute'); $ This- & gt; Forward ($ clientActionRoute, array ('request' = & gt; $ request)); } thanks
For me, it's kind of a To use such methods oddly (uploading files directly to third party resources) why not just use bundles: You can configure as many adapters as you need, but in this case, you can You will be able to test your entire application / project by changing the adapter configuration for test / dev env.
Comments
Post a Comment