forward() including form data

2 months ago

dakur
Member | 44
+
0
-

Hi,

just wondering – is there a way to $this->forward() in presenter to another presenter with form data in the request included? Specifically a file.

I have two presenters – one uploads an image to backend, second downloads an image from remote server and then uploads it to backend as well. I do not want to repeat my code so I decided to get use of first presenter in the second by passing downloaded image to it. But as the second presenter takes form data on input, I don't how to deliver the file to it with forward() – I need something like cURL in the forward() method. Is it clear and is this possible?

I know I can extract common behaviour to separate class/function/whatever, but that does not fit my case. I have already extracted it and the code left is just condition checking (authenticated, existence etc.) which should reside in the presenter itself.

I could also post request the presenter via standard cURL, but the target (second presenter) is hidden behind authentication so it's no-go.

Thanks for any advice.

Example what I mean:

// PSR way to post image to endpoint
$httpClient->sendRequest(new Request('POST', $this->link('//Presenter:action'), [], $downloadedImageAsAStream));

// What I am trying to achieve
$this->forward('//Presenter:action', [...$optionalParams], $downloadedImageAsAStream); // third parameter does not exist now and probably does not make sense much

Last edited by dakur (2020-06-18 14:10)

2 months ago

David Grudl
Nette Core | 7148
+
0
-

What about

$this->sendResponse(new Nette\Application\Responses\ForwardResponse(
	new Request('POST', $this->link('//Presenter:action'), [], $downloadedImageAsAStream)
));

2 months ago

dakur
Member | 44
+
0
-

@DavidGrudl Yeah, that works, thanks very much!

$this->sendResponse(new \Nette\Application\ForwardResponse(
    new \Nette\Application\Request('Presenter', 'POST', [...$optionalParams], [], [$downloadedImageAsAStream]),
));

Dunno how to specify target action on the presenter, but I don't need it anyway as I have default action.

2 months ago

David Grudl
Nette Core | 7148
+
0
-

As Request('Presenter', 'POST', ['action' => '...'])

2 months ago

dkorpar
Backer | 94
+
0
-

IMHO you shouldn't have logic in presenter but rather in some class outside and then you can call it from any presenter…

2 months ago

dakur
Member | 44
+
0
-

@DavidGrudl Aha, thanks.

@dkorpar As I wrote:

I have already extracted it and the code left is just condition checking (authenticated, existence etc.) which should reside in the presenter itself.