Search code examples
phpzend-framework2

Exceeding 10 nested forwards?


My project requires multiple payment gateways, so I set up a module per payment gateway and bound the appropriate payment gateway to the account that was using it. I then placed all payment code within the appropriate module controller. All gateways implement the same interface to ensure functions exist.

I am now running a nightly cron to charge payments against each account. The issue I'm getting is that in order to get the appropriate controller to run, I am using the following code inside a foreach loop:

$response = $this->forward()->dispatch(sprintf('PaymentGateway\%s\Controller\Index', ucwords($pg->code)), $params);

where $pg->code is the payment gateway assigned to the account.

At the moment I have two payment gateways (this will be going up in the near future) with about 20 test payments to be processed each. When I run the cron command I am getting the error:

Circular forwarding detected: greater than 10 nested forwards

In this scenario I need it to run 40 times, but that will go up in the future as more payment gateways are added and more transactions are queued.

In terms of a solution, I am looking to either

  • disable this check for the purposes of my cron or,
  • find an alternative to $this->forward()->dispatch(...); that will still allow me to call a different controller multiple times with custom parameters.

The alternative is that I pass in all transactions to each respective payment gateway and only call $this->forward()->dispatch() once per gateway, but I'll hit this issue again when I get to ten payment gateways.


Solution

  • The forward controller plugin allows you to set the maximum number of nested forwards.

    Before my loop I set the max number of nested forwards to the number of items I was going to loop through:

    $this->forward()->setMaxNestedForwards(count($transactions));