i have two controllers: firstController and SecondController. In the firstController I have to call a function of the secondController, passing the parameters.
How can you do? Thanks
i have two controllers: firstController and SecondController. In the firstController I have to call a function of the secondController, passing the parameters.
How can you do? Thanks
Calling an action on a controller is called dispatching. You can also dispatch a controller within a controller, which is called forwarding. There is a controller plugin available to forward to another controller.
An example:
class FirstController extends AbstractActionController
{
public function fooAction()
{
$result = $this->forward()->dispatch('SecondController', array(
'action' => 'bar',
));
// $result is ViewModel with parameter bar = "baz"
}
}
class SecondController extends AbstractActionController
{
public function barAction()
{
return new ViewModel(array(
'bar' => 'baz',
));
}
}
If you leave the second argument to dispatch() out, it will use the current parameters of the primary controller.
i think this is a bad idea to do that
in addition to the redispatching solution, why don't you try to create a service for your need (just a class with needed methods) and use it in your controllers
or a plugin if you will use it frequently in your controllers ( or register event listners and trigger events in your controllers if you need to do a job in a larger scale or anywhere in your app)
however you can get your controller from the controllerLoader and call its methods (but in my opinion, you're beginning spaghetti coding if you can't find any alternative to do that)