Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to call another controller action from a controller in ZF2

What is the best way of calling an action of another controller?

I am thinking in different options:

  1. Using forward plugin
  2. Setting the second controller as a dependency in the first controller

I think the first option will work but I would like to know if the second one has sense. In that case,

  1. How can I get the controller dependency?

I have tried to get it using the Service Manager but it isn't there. I have tried creating a Factory for the controller and get the another controller from the Controller Manager which is passed as an argument to the createService method when you implement the FactoryInterface. But it says that no controller is there. Exists a different locator from controllers than can be used in the same way as the Service Locator?

  1. The controller which has the role of dependency and will be called has to extends or implements any particular class or interface?
like image 865
itrascastro Avatar asked Dec 15 '22 16:12

itrascastro


1 Answers

To forward with parameters :

$this->forward()->dispatch('Namespace\Controller', array(
            'action' => 'ActionName',
            'params' => array(
                'id' => $id
            )
        ));
like image 138
Ritesh Avatar answered Apr 27 '23 01:04

Ritesh