Search code examples
phpphalcon

How to invalidate the template once set?


Question

Please tell me how to invalidate the template once set with the setTemplateAfter method on Phalcon 3 so as not to refer to the template.


Overview

The current configuration is extended by IndexController ControllerBase.

In the initialize method of ControllerBase, set the template with setTemplateAfter as follows.

$this->view->setTemplateAfter('common');

In the expanded IndexController, since the template is unnecessary, an error occurs although the following is executed.

$this->view->setTemplateAfter('');

*I am thinking that I do not want to change to ControllerBase as much as possible because the template is used by another controller.


Error message

View 'layouts/' was not found in any of the views directory
#0 [internal function]: Phalcon\Mvc\View->_engineRender(Array, 'layouts/', false, true, NULL)
#1 [internal function]: Phalcon\Mvc\View->render('index', 'index')
#2 /mnt/raid/serverapps/www/lashca/public/index.php(42): Phalcon\Mvc\Application->handle()
#3 {main}

Source code

controllers/ControllerBase.php

<?php
use Phalcon\Mvc\Controller;
class ControllerBase extends Controller
{
    public function initialize()
    {
        $this->view->setTemplateAfter('common');
    }
}

controllers/IndexController.php

<?php
use Phalcon\Mvc\Controller;
class IndexController extends ControllerBase
{
    public function indexAction()
    {
        $this->view->setTemplateAfter('');
    }
}

Environment

  • CentOS 7.4(x64)
  • Apache 2.4.6
  • PHP 7.0.26
  • Phalcon Framework 3.2.4
  • MySQL Ver 14.14 Distrib 5.6.38

Solution

  • You can disable certain levels of view rendering via $this->view->disable().

    For example, if you want to disable the "template after"-rendering you can do this:

    $this->view->disableLevel([
        View::LEVEL_AFTER_TEMPLATE  => true
    ]);
    

    Check the documentation if you want to disable other levels of the view.