You can use the Zend\Mvc\Controller\Plugin\Forward
controller plugin to dispatch another controller action from within another.
The docs say
Occasionally, you may want to dispatch additional controllers from within the matched controller – for instance, you might use this approach to build up “widgetized” content. The Forward plugin helps enable this.
This is useful if you have already have these actions but would like to combine them with others to build an aggregated view.
use Zend\View\Model\ViewModel;
class AdminController extends AbstractActionController
{
public function adminDashboardAction()
{
$view = new ViewModel();
$view->setTemplate('admin/admin/dashboard');
//..
$serverStatsWidget = $this->forward()->dispatch('ServiceModule\Controller\Server', array(
'action' => 'status',
'foo' => 'bar',
));
if ($serverStatsWidget instanceof ViewModel) {
$view->addChild($serverStatsWidget, 'serviceStats');
}
return $view;
}
As $serverStatsWidget
is the result of the dispatched controller, you can then add it to the 'main' view as a child and render the result just by using echo
.
// admin/admin/dashboard.phtml
echo $this->serviceStats;