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

$this->view->disable() still load compiled layout

Hello, I made a dynamic robots.txt controller and I have this code:

    public function robotsAction()
    {
        $this->view->disable();
        $this->response->setHeader("Content-Type", "text/plain");
        $this->response->setContent("User-agent: *");
        $this->response->send();
    }

Problem is that in errorlog I see: "2016/10/25 22:21:47 [error] 22649#0: 9295 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined variable: location in /var/www/cache/volt/_var_www_main_app_views_index.volt.php on line 223"

So, view is still processed but not rendered, is any way to disable this ? Thanks!



6.0k

Yes, I did check, but I don't want pemanent disable, I added disable render code in view service and is true that it won't compile anymore but ... for all controllers. I need this only for one controller/action, I tried several methods but layout is still compiling.

edited Oct '16

Change your code to:

$this->response->setHeader("Content-Type", "text/plain");
return $this->response->setContent("User-agent: *");

Even disable is not needed.

Also you sure that this action is hitted and there is no some forward or something ? /var/www/cache/volt/varwwwmainappviewsindex.volt.php auto rendering would never render index.volt.php from robotsAction anyway



6.0k

Thanks for response, but I still see template notices (they are still loaded). This is my view service


$di->set('view', function () {
    $config = $this->getConfig();
    $view = new View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines([
        '.volt' => function ($view) {
            $config = $this->getConfig();
            $volt = new VoltEngine($view, $this);
            $volt->setOptions([
                'compiledPath' => $config->application->cacheDir . 'volt/',
                'compiledSeparator' => '_'
            ]);
            $compiler = $volt->getCompiler();
            $compiler->addFunction("shuffle", "shuffle");
            $compiler->addFunction(
                'lang',
                function () {
                    return 'Vokuro\Utils\MyTags::lang()';
                }
            );
            return $volt;
        }
    ]);

    $eventsManager = new Phalcon\Events\Manager();
    $eventsManager->attach("view", new \Vokuro\Utils\RenderView());
    $view->setEventsManager($eventsManager);

    return $view;
}, true);

No matter level of rendering set in method, beforeRender is still called. With: https://docs.phalcon.io/uk/latest/reference/views.html#disabling-render-levels this code added in service, no layout is loaded, only controller/action block.

edited Oct '16

Better show dispatcher code. Check if this action is even executed by die(); on beginning or xdebug breakpoint. The code i wrote for 100% can't hit view anywhere internally. I have like most of action returning json and im not disabling view anywhere. To prove it:

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/application.zep#L300

As you see, view is hitted only in else condition. If you return response object from action i posted view is not even hitted.



6.0k
/**
 * Dispatcher use a default namespace
 */
$di->set('dispatcher', function () {
    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultNamespace('Vokuro\Controllers');

    $eventsManager = new Manager();
    $eventsManager->attach("dispatch:beforeDispatchLoop", new Vokuro\Locale\Localisation());

    $eventsManager->attach("dispatch:beforeException", function($event, $dispatch, $exception) {
        if ($event->getType() == 'beforeException') {
            switch ($exception->getCode()) {
                case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                $dispatch->forward(array(
                        'controller' => 'index',
                        'action' => 'notFound'
                    ));
                    return false;
            }
        }
    });
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

Added first line in robotsAction - die(); - still rendering in errorlog. /var/www/cache/volt/varwwwmainappviewsindex.volt.php - this is main layout containing only header/{{ content() }}/footer

edited Oct '16

As i posted already. The code above should work for 100%. Are you sure you checked my code ?

As you see here - https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/application.zep#L307 if response object is returned from action else(which can call render is not even hitted).

If it's not working then problem is somewhere else, possible some forward, or you just call render yourself, not robotsAction, what's the content of Vokuro\Locale\Localisation ?

Also if this still rendering in errorlog it means that problem is somewhere BEFORE robotsAction, because render from robotsAction could only happen if nothing is returned from controller action.

Also you should use your own namespace instead of Vokuro :D



6.0k

LOL, this caused the problem:


class ControllerBase extends Controller
{
    public function initialize()
    {
        if(!$this->request->isAjax()){
            $this->assets
                ->collection("header_js")
                ->setPrefix("//{$this->config->static}/")
                ->setLocal(false)
                ->addJs("js/jquery.min.js")
                ->addJs("js/bootstrap.min.js")
                ->addJs("js/custom.js");
            $this->assets
                ->collection("header_css")
                ->setPrefix("//{$this->config->static}/")
                ->setLocal(false)
                ->addCss("css/bootstrap.min.css");
                .....
            }
        }
    }
}

I deleted $this->assets code and rendering level worked. Is this a bug ?



145.0k
Accepted
answer
edited Oct '16

No it's not. Assets manager is not calling view anywhere. Problem must be somewhere else.



6.0k

Problem was the buggy favicon .... missing and no rules in nginx. Thanks for help.