0

I have a form with text fields and one image file . I want to upload the image and next save the form information in the database. I have two actions : Create and UploadImage. I want to call the execute method of UploadImage action in the execute method of Create. The idea is upload the file first and next create the fields.

It is possible to inject the UploadAction in my Create and call the execute method of UploadAction in the execute method of Create?

1

I's not a good idea to call controller actions inside other controller actions. Not sure if it's even possible. May be.
But here is what you can do.
Create a separate class that handles the image upload that should not me a controller.
This class should receive some parameters and work with those.
In the UploadImage controller, read the files that are being uploaded and pass them to your class as the parameters I mentioned above.
This way you can use your upload class in the UploadImage controller and in any other controller you like.

  • I already try this , i create a class not extend of Action, just a normal php class with a method call upload , but when i call this method in the controller action create dont get the uploader value in $uploader = $this->_fileUploaderFactory->create(['postImage' => 'name']); , is empty always – Andres Franco Feb 21 '18 at 15:49
0

I kind of agree with Marius, but you can use the _redirect method in your controller to point to another controller and pass arguments:

enter image description here

  • wouldn't this make a redirect in the browser? – Marius Feb 21 '18 at 15:45
  • Do you have an example when you execute a Controller action and redirect to other controller action after complete the first action? – Andres Franco Feb 21 '18 at 15:56
  • This will redirect in the browser, so take it for what it's worth. Here's an example: github.com/magento/magento2/blob/2.2-develop/app/code/Magento/… – Ethan Yehuda Feb 21 '18 at 16:09
  • I iam calling the action in ajax with jquery. is working in ajax too? – Andres Franco Feb 21 '18 at 16:40
  • It should work as long as you hit the action path. It's hard to say without looking at your controller's code – Ethan Yehuda Feb 21 '18 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.