Well, you linked to code that is running in Listeners and you posted code that appears to be running in a controller. It would be helpful if we had some more code so we could see exactly what you are trying to accomplish and what its context is.
The [controller] is missing from the component’s output.
Should that be available, and if not, is it possible to identify the controller of a child of a view?
<?php
namespace Application\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function componentAction(){
$event = $this->getEvent();
echo "<pre>";
print_r($event->getRouteMatch());
echo "</pre>";
return new ViewModel();
}
public function exampleAction(){
$event = $this->getEvent();
echo "<pre>";
print_r($event->getRouteMatch());
echo "</pre>";
$component = $this->forward()->dispatch(IndexController::class, array('action' => 'component'));
$view = new ViewModel();
$view->addChild($component, 'myComponent');
return $view;
}
}
I think you may might be misunderstanding exactly what is occurring here.
You are taking the returned response from executing (dispatching) the component action and attaching it to the examples ViewModel so as to have a means to render it within the example actions response. That does not make the component action a child of example. Those actions are siblings if you want to view it that way because they share the same controller. Look at it as creating a “widget” from the component action.
If you have xdebug setup you can set a breakpoint in the forward controller plugin and introspect what data you have here:
Then just step through it and see if you have the controller in $params (since this is what is used to seed the next RouteMatch. You know the controller because you are passing the controller class name to forward and in the action that you are forwarding to it would be available as __ CLASS __.
It’s been awhile since I used the MVC but you might also want to check in the component action via $this->params() to see if the controller name is present.
A completely different view: don’t use the forward and forget the crap! This is because a route refers to one endpoint in your application, e.g. a controller action or a request handler. If you want to add components or extend the view model, it is not necessary to call another endpoint in your application. There are other and better solutions for this, and the overhead of the dispatch process is not necessary.
Thanks for your help on this, and yes, I am probably not understanding what’s going on.
My sample code does have the component action from the same controller, but this was just to keep the code simple.
The idea is that I can have a widget taken from any controller and reuse it throughout my application. This approach seems to be how to do it in laminas mvc as the docs refers to “widgetized” content.
The $params only has the action, unless I specify otherwise, e.g.
I’ll see what__CLASS__ does for me and see if that helps me identify the controller of my component.
I did implement a workaround using matchedRouteName, as it was consistently set regardless of whether I navigated to the componentAction() directly or via forward()->dispatch().
I’m not sure of what you are trying to accomplish but as @froschdesign mentioned earlier. There is much better ways to accomplish it. By doing this you are incurring the overhead of executing two controllers, the primary controller that is dispatched to your route, plus the “widget” controller that you are forwarding too.
As a side note. If this is a new application I would start it using Mezzio and not the MVC.