Cascading Views Folders

Cascade Views folders in phalcon 3, Views Cascading is to handle with multiple views folder, and then we say to phalcon that " when you don't find templates files in first folder then look into to the next folder". to do this we have to extend phalcon view class

let see the steps to cascade views,

 

Created this file in custom/CustomView.php

 

<?php

 use Phalcon\Mvc\View\Exception;
 use Phalcon\Mvc\View;
 use Phalcon\Cache\BackendInterface;

 class CustomView extends View
 {
     protected $_viewsDirs;
     /**
     * @var
     */
     protected $_eventsManager;
    /**
     * @param $path
      *
     * @return $this
     */
      public function addViewsDir($path)
     {
       $this->_viewsDirs = $path;
       $this->setViewsDir($path);
       return $this;
     }
     /**
     * @param $view
     * @param array $vars
     *
     * @return string
     */
     public function getPartial($view, $vars = [])
     {
         ob_start();
         $this->partial($view, $vars);
      $content = ob_get_contents();
      ob_end_clean();
         return $content;
      }

      protected function _engineRender($engines, $viewPath, $silence, $mustClean, BackendInterface $cache = NULL)
      {
        if (is_object($cache)) {
            throw new Exception('Cache view not supported...');
            return;
        }
        $viewsDirs = is_array($this->_viewsDirs) ? array_reverse($this->_viewsDirs) : [$this->_viewsDir];
        $notExists = true;
        $viewEnginePath = null;
        foreach ($engines as $extension => $engine) {
            foreach ($viewsDirs as $viewsDir) {
                $viewsDirPath   = $this->_basePath . $viewsDir . $viewPath;
                $viewEnginePath = $viewsDirPath . $extension;
                if (is_file($viewEnginePath)) {
                    if (is_object($this->_eventsManager)) {
                        $this->_activeRenderPath = $viewEnginePath;
                        if($this->_eventsManager->fire('view:beforeRenderView', $this, $viewEnginePath) === false) {
                            break;
                        }
                    }
                    $engine->render($viewEnginePath, $this->_viewParams, $mustClean);
                    if (is_object($this->_eventsManager)) {
                        $this->_eventsManager->fire('view:afterRenderView', $this);
                    }
                    $notExists = false;
                    break 2;
                }
            }
        }
        if ($notExists) {
            if (is_object($this->_eventsManager)) {
                $this->_activeRenderPath = $viewEnginePath;
                $this->_eventsManager->fire('view:notFoundView', $this);
            }
            if (!$silence) {
                $exceptionMessage = 'View "'.($viewPath).'" was not found in the views directories';
                throw new Exception($exceptionMessage);
                return;
               }
            }
        }
}

 

add this to  your public/index.php

<?php
use Phalcon\Loader;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
use Custom\CustomView;

try {

  $loader = new Loader();
  $loader->registerNamespaces(array(
  'Custom'  => 'custom'
  ))->register();


$di = new FactoryDefault();


$di->set(
'view',
  function () {
    $view = new CustomView();
    $view->addViewsDir(array('../app/common/views/','../app/anotherdirectory/views/'));
    $view->registerEngines(
      array(
      ".volt" => 'voltService'
      )
    );
    return $view;
  }
);

$application = new Application($di);
$response = $application->handle();
$response->send();

}
catch (\Exception $e) {
  echo "Exception: ", $e->getMessage();
}

 

We are just Calling our class Customview in Phalcon Bootstrap. thats it.

 

Now you can have two view directory,  application will first look into  anotherviewsdirectory then look into common views directory