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

[solved] Who call the $view->render() ?

Hi all, I'm experiencing a strange behaviour. If I put a logger inside the render() function of the $view (I've extended it), I see that the render() is called several times. The final result displayed is ok (I mean the template is not rendered twice), but the function render() is called too many times and it should be called one. Who's the caller of $view->render()? Have you got any ideas about this behaviour?

That's my render() function log extract for only one page view:

[2017-10-05 19:48:49][local][WARNING] rendering: index, index
[2017-10-05 19:48:49][local][WARNING] rendering: error, show404
[2017-10-05 19:48:50][local][WARNING] rendering: error, show404
[2017-10-05 19:48:50][local][WARNING] rendering: error, show404
[2017-10-05 19:48:50][local][WARNING] rendering: error, show404
[2017-10-05 19:48:50][local][WARNING] rendering: error, show404
[2017-10-05 19:48:51][local][WARNING] rendering: index, index
[2017-10-05 19:48:54][local][WARNING] rendering: index, getcartitemn
[2017-10-05 19:48:54][local][WARNING] rendering: error, show404
[2017-10-05 19:48:56][local][WARNING] rendering: index, index

Hi @cosmy81 do you have any redirection? views are rendered even after you call dispatcher->redirect().

You must disable a render i.e. using $this->view->disable()in your controller

Good luck



12.1k
edited Oct '17

Hi Emilio, I've found what's the matter with my app. It was my fault. The frontend module was loaded manually in my Bootstrap so it runs several times, calling the dispatcher many times, and so the view rendering. But at the very end there were only one response send and the view is displayed once.
wrong code:

    $application->registerModules(require BASE_PATH .'/config/modules.php');
    require(APP_FRONTEND_PATH . 'Module.php');
    $module = new App\Frontend\Module(); //sbagliato!! lo lancia in automatico registerModules
    $module->registerAutoloaders($di); //sbagliato!! lo lancia in automatico registerModules
    $module->registerServices($di); //sbagliato!! lo lancia in automatico registerModules
    if(PHALCON_DEBUG)
        $this->initDebug($di);
    $response = $application->handle();
    $response->send();

right code:

    $application->registerModules(require BASE_PATH .'/config/modules.php');
    require(APP_FRONTEND_PATH . 'Module.php');
    if(PHALCON_DEBUG)
        $this->initDebug($di);
    $response = $application->handle();
    $response->send();

So, to reply to my topic question, the dispatcher invoke the view render. And the application invoke the module and the dispatcher (through the handle() module) generating the response. The module register the dispatcher and the view services.
In my case the response is finally sent by the Bootstrap init.

I think that the documentation should contain a graphic flow summary to be immediatly clear. If you want I can do that, even if I think there sre a lot of things that I still don't know.