Search code examples
phpphalconvolt

Volt directory can't be written


The error I am getting is

Warning: Phalcon\Mvc\View\Engine\Volt\Compiler::compileFile(../app/views/index/index.phtml.php): failed to open stream: Permission denied in /Users/mattstephens/Sites/magpie/public/index.php on line 26 Phalcon Exception: Volt directory can't be written

I have declared the volt engine usage in my bootstrap like so

$view->registerEngines(array(
      '.phtml' => 'Phalcon\Mvc\View\Engine\Volt'
    ));

The mention of line 26 in my code points to the application handle function shown below

echo $application->handle()->getContent();

Is this a permissions related thing or due to a missing directory?


Solution

  • Unless you specify a different folder for Volt to compile its templates, the folder where the view file is located will be used to create the relevant compiled file.

    You can change this behavior by setting the proper option when you register your service as such:

    use \Phalcon\Mvc\View as PhView;
    use \Phalcon\Mvc\View\Engine\Volt as PhVolt;
    
    ...
    
    public function initView($options = array())
    {
        $config = $this->di['config'];
        $di     = $this->di;
    
        $this->di['volt'] = function ($view, $di) use ($config) {
    
            $volt = new PhVolt($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['view'] = function () use ($config, $di) {
    
            $view = new PhView();
            $view->setViewsDir($config->app_path->views);
            $view->registerEngines(array('.volt' => 'volt'));
    
            return $view;
        };
    }
    

    The $config will store all the information you need. By using the compiledPath you instruct Volt to compile the templates there and then serve them to the front end. That folder needs to be writeable for the user that runs your web server www-data or other and can be outside your public folder.

    The file structure I usually use is:

    app
        \controllers
        \models
        \views
    public
        \js
        \css
        \img
    var
        \volt
        \logs
        \config
        \cache