How do I set the volt compiled directory path in Phalcon?
Question
I have the following code:
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views/');
$view->registerEngines(array(
".phtml" => 'Phalcon\Mvc\View\Engine\Volt'
));
return $view;
});
But now the compiled PHP lives in the views directory. How can I set a different path for the compiled directory?
Solution
You can set the new compiledPath
and other options as follows:
Assume that you have these variables in your config:
[views]
path = '/home/user/www/app/views/'
[volt]
path = '/home/user/www/app/volt/'
extension = '.compiled'
separator = '%%'
stat = 1
You can then do this according to the manual:
// Assuming that this is in a class and `_di` is your DI container
$config = $this->_di->get('config');
$di = $this->_di;
/**
* Setup the volt service
*/
$this->_di->set(
'volt',
function($view, $di) use($config)
{
$volt = new Volt($view, $di);
$volt->setOptions(
array(
'compiledPath' => $config->app->volt->path,
'compiledExtension' => $config->app->volt->extension,
'compiledSeparator' => $config->app->volt->separator,
'stat' => (bool) $config->app->volt->stat,
)
);
return $volt;
}
);
/**
* Setup the view service
*/
$this->_di->set(
'view',
function() use ($config, $di)
{
$view = new \Phalcon\Mvc\View();
$view->setViewsDir(ROOT_PATH . $config->app->path->views);
$view->registerEngines(array('.volt' => 'volt'));
return $view;
}
);
or you can follow the implementation below (the above one is preferred)
$di->set('view', function() use ($config, $di) {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir($config->views->path);
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(
array(
'compiledPath' => $config->volt->path,
'compiledExtension' => $config->volt->extension,
'compiledSeparator' => $config->volt->separator,
'stat' => (bool) $config->volt->stat,
)
);
/**
* Register Volt
*/
$view->registerEngines(array('.volt' => $volt));
return $view;
});
Make sure that your $config->volt->path
is writeable. You don't need to follow exactly the approach above - you can always substitute the configuration variables with whatever your application needs.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow