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

Get controller result from different module

Hi, is there any way how you can get result of controller action from different module? I would like to create multirequest action which will handle multiple request in single request - but when I try call dispatcher I get always error. I tried setModuleName on dispatcher, tried create new application instance and handle request but still have same problem. What I need is take response from multiple module and put it to array as json output. Do I need clone dispatcher on top level - in application?Thanks for hints.

edited Sep '14

Oki probably found a solution:

$di->setShared('appDispatcher', $di->get('dispatcher'));

in AbstractController i declare request function which is originally defined here: https://github.com/phalcon/mvc/blob/master/hmvc/public/index.php#L49

    public function request($location)
    {
        /** @var Dispatcher $dispatcher */
        $dispatcher = clone $this->getDI()->get('appDispatcher');

        if (isset($location['namespace'])) {
            $dispatcher->setDefaultNamespace($location['namespace'] . '\\Controllers');
        }

        if (isset($location['controller'])) {
            $dispatcher->setControllerName($location['controller']);
        } else {
            $dispatcher->setControllerName('index');
        }

        if (isset($location['action'])) {
            $dispatcher->setActionName($location['action']);
        } else {
            $dispatcher->setActionName('index');
        }

        if (isset($location['params'])) {
            if (is_array($location['params'])) {
                $dispatcher->setParams($location['params']);
            } else {
                $dispatcher->setParams((array)$location['params']);
            }
        } else {
            $dispatcher->setParams(array('multiRequest' => true));
        }

        $dispatcher->dispatch();

        $response = $dispatcher->getReturnedValue();
        if ($response instanceof ResponseInterface) {
            return $response->getContent();
        }

        return $response;
    }

afterExecuteRoute will simply handle multiRequest by forwarded param

    public function afterExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher)
    {
        $params = $dispatcher->getParams();

        $data = $dispatcher->getReturnedValue();
        if (is_array($data)) {
            $data = json_encode($data);
        }

        if (!isset($params['multiRequest'])) {
            $this->view->disable();
            $this->response->setHeader('Cache-Control', 'no-cache, must-revalidate, post-check=0, pre-check=0');
            $this->response->setExpires(new \DateTime('Mon, 26 Jul 1997 05:00:00 GMT'));
            $this->response->setContentType('application/json', 'UTF-8');
            $this->response->setContent($data);
            $this->response->send();
        }
    }

and finally multirequest action:

    $router = $this->router;
    $router->handle($requestParams);

    $r = $this->request(array(
        'namespace' => $router->getModuleName(),
        'controller' => $router->getControllerName(),
        'action' => $router->getActionName()
    ));