I'm using the HMVC setup as found here, but when I follow through with it's use, it's happily responding with the called response content, but it's ignoring the parent controller view file.
my "parent" controller has:
public function indexAction()
{
$widgets[] = $this->app->request(
[
"namespace" => "sf\Test\Controllers",
"controller" => "Test",
"action" => "testwidget",
]);
$widgets[] = $this->app->request(
[
"namespace" => "sf\Test2\Controllers",
"controller" => "Test2",
"action" => "test",
]);
$this->view->setVar('widget', $widgets);
}
I've extended the request function slightly to take account of the namespaces but I think it's essentially the same:
<?php
use Phalcon\DiInterface;
use Phalcon\Mvc\Application as MVCApplication;
class HMVCApplication extends MVCApplication
{
/**
* HMVCApplication Constructor
*
* @param DiInterface $di
*/
public function __construct(DiInterface $di)
{
// Register the app itself as a service
$di["app"] = $this;
// Sets the parent Id
parent::setDI($di);
}
/**
* Does a HMVC request in the application
*
* @param array $location
* @param array $data
*
* @return mixed
*/
public function request(array $location, $data = null)
{
$di = $this->getDI();
$dispatcher = clone $di->get("dispatcher");
if (isset($location["namespace"])) {
$dispatcher->setNamespaceName($location["namespace"]);
}
if (isset($location["controller"])) {
$dispatcher->setControllerName($location["controller"]);
} else {
$dispatcher->setControllerName("index");
}
if (isset($location["action"])) {
$dispatcher->setActionName($location["action"]);
} else {
$dispatcher->setActionName("index");
}
$params = [];
if (isset($location["params"])) {
if (is_array($location["params"])) {
$dispatcher->setParams($location["params"]);
} else {
$dispatcher->setParams(
(array)$location["params"]
);
}
}
$dispatcher->setParams($params);
$dispatcher->dispatch();
$response = $dispatcher->getReturnedValue();
if ($response instanceof ResponseInterface) {
return $response->getContent();
}
return $response;
}
}
$application = new HMVCApplication($di);
For eah of the test functions I've got the following code:
public function testwidgetAction()
{
echo("hello world!");
}
[...]
public function testAction()
{
echo "Another test!!";
}
Now my "parent" view file looks like:
<h1>Dashboard</h1>
Widget: {% for widget in widgets %}
{{ widget }}
{% endfor %}
Now, I was expecting to get the whole h1 with the Dashsboard text followed by each of the widgets that I'm calling. howver i'm only getting the widgets responses.
Now I think that this has to do with the shared repsonse system, and therefore as soon as the first widget is called it's superceeding the parent controller, but then the subsequent wiget calls are "extending" the response with their views.
So my root question is: how do I get both the parent view and the subsequent widget views to show on the screen?
Any help or advice would be greatly appreciated.
Many thanks in advance.