We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

hmvc example from phalcon/mvc

I just learned about hmvc, excellent concept, and played around the phalcon example. The problem with the example is the sub controller calls don't load or render views. Here is the Index/index controller/action (I've added corresponding view directories and files):

// IndexController
public function indexAction()
{
    // does a request calling say/hello
    $helloWidget = $this->app->request(array(
        'controller' => 'say',
            'action' => 'hello',
    ));

    $this->view->setVar('hello', $helloWidget);
}

Controller "say", action "hello", $this is the same as the caller IndexController

// SayController
public function helloAction()
{
    echo '<pre>----', PHP_EOL;

    echo $this->view->getControllerName(), PHP_EOL; // (empty)
    echo $this->app->view->getControllerName(), PHP_EOL; // (empty)

    echo $this->dispatch->getControllerName(), PHP_EOL; // index
    echo $this->dispatch->getActionName(), PHP_EOL; // index

    echo $this->app->dispatch->getControllerName(), PHP_EOL; // index
    echo $this->app->dispatch->getActionName(), PHP_EOL; // index

    exit;
}

It can be made to render the view manually:

// SayController
public function helloAction()
{
    $controller = strtolower(str_replace('Controller', '', get_class($this)));
    $action = str_replace('Action', '', __FUNCTION__);

    $viewsDir = $this->app->view->getViewsDir(); // One and the same as $this->view, change one, all will change
    $view = new Phalcon\Mvc\View\Simple();

    $var = '... Hello?';

    return $view->render($viewsDir . $controller . '/'. $action, ['var' => $var]);
}

To make use this for all controllers, I could move this to a ControllerBase class, and make a method named maybe renderView($params) which I will need to call in sub Controllers, some changes to __FUNCTION__ is needed but it works, end up with.

// SayController
    public function helloAction()
    {
        $var = '... Hello?';

        return $this->renderView(['var' => $var]);
    }

Even though it works, I donno, doesn't feel robust, I get the feeling I'm better off using regular MVC in Phalcon. Anybody actually doing it HMVC style in Phalcon?

I have the same problem. I feel bad when I call the view manually.