Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to configure Volt's compiledPath folder

Me again (i'm probably the biggest newbie in this forum), i'm having trouble trying to add Volt's compiledPath folder into my config file (index.php), here is the code of my index.php:

<?php

try {

    //Register an autoloader
    $loader = new \Phalcon\Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
        '../app/models/'
    ))->register();

    //Create a DI
    $di = new Phalcon\DI\FactoryDefault();

    //Set the database service
    $di->set('mongo', function() {
        $mongo = new Mongo();
        return $mongo->selectDb("sample_db");
    }, true);

    //Setting up the view component
    $di->set('view', function(){
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../app/views/');

        #ativando a templateEngine Volt
        $view->registerEngines(array(
            ".phtml" => 'Phalcon\Mvc\View\Engine\Volt'
        ));

        return $view;
    });

    $di->set('collectionManager', function(){
        return new Phalcon\Mvc\Collection\Manager();
    }, true);

    //Handle the request
    $application = new \Phalcon\Mvc\Application();
    $application->setDI($di);
    echo $application->handle()->getContent();

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

How exacly do i configure the compiledPath into this file? I've tried registering:

$di->set('volt', function($view, $di) {
    $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
    $volt->setOptions(array(
        "compiledPath" => "../app/cache/volt/"
    ));
    return $volt;
}, true);

before it but it didn't work. And i don't understand well why. I need to create another kind of config file just for this $di->set('volt' ?



85.4k
Accepted
answer

Hey Claudio, instead of doing this:

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

do this:

$view->registerEngines(array(
      ".phtml" => function($view, $di) {
           $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
           $volt->setOptions(array(
                "compiledPath" => "../app/cache/volt/"
           ));
           return $volt;
    }
));

Perfect, worked perfectly. Again, thanks!

Is this in the documentation anywhere? I had the same question but none of the code examples for when registering Volt as a template engine specified you could use a lambda function instead of a string.

I am also having a problem getting Volt to work. First of all, if you want to recognize the .volt extension, then why would you use .phtml when registerimng the engine: ".phtml" => function($view, $di)

Also, I am new to Phalcon, but have worked with MVC frameworks for a while and never had anywhere near this amount of difficulty getting a templating engine to work. The documentation is pretty much worthless other than the vague code snippets which are not really described well.

I have a simple CRUD app and I want to convert the phtml files in the view to Volt files. I am using the above mentioned code snippet from the Volt documentation page and then I get a "volt directory can't be written to" error message. I then created a "volt" folder in a "cache" folder under the "app" directory, set permissions to 777, and pointed my 'compiledPath' to it yet that doesn't work. This is simply ridiculous! Could someone please explain, step-by-step, in 6th grade language, what exactly needs to be done to get Volt to work in a fresh Phalcon project. I am literally pulling my hair out.

Thank you for in advance for any help you can give.



31.2k

Hi,

I would suggest you to use phalcon developer tools to generate a basic app skeleton (wich will be volt enabled) and create all the crud controllers/actions you need.

Otherwise, have a look at all the demo apps that are available on phalcon github area (like that forum, invo, vokuro ... they all use volt template engine).

good luck



31.2k

// app/config/config.php

return new \Phalcon\Config([
    'database' => [
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => '',
        'password'    => '',
        'dbname'      => 'blog',
        'charset'     => 'utf8',
    ],
    'application' => [
        'appDir'         => APP_PATH . '/',
        'controllersDir' => APP_PATH . '/controllers/',
        'modelsDir'      => APP_PATH . '/models/',
        'migrationsDir'  => APP_PATH . '/migrations/',
        'viewsDir'       => APP_PATH . '/views/',
        'pluginsDir'     => APP_PATH . '/plugins/',
        'libraryDir'     => APP_PATH . '/library/',
        'cacheDir'       => BASE_PATH . '/cache/',
        'baseUri'        => '/',
    ]
]);

// app/config/services.php

/**
 * Setting up the view component
 */
$di->setShared('view', function () {
    $config = $this->getConfig();

    $view = new Phalcon\Mvc\View();
    $view->setDI($this);
    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines([
        '.volt' => function ($view) {
            $config = $this->getConfig();

            $volt = new Phalcon\Mvc\View\Engine\Volt($view, $this);

            $volt->setOptions([
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ]);

            return $volt;
        },
        '.phtml' => PhpEngine::class

    ]);

    return $view;
});

// public/index.php

<?php
use Phalcon\Di\FactoryDefault;

error_reporting(E_ALL);

define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');

try {

    /**
     * The FactoryDefault Dependency Injector automatically registers
     * the services that provide a full stack framework.
     */
    $di = new FactoryDefault();

    /**
     * Read services
     */
    include APP_PATH . "/config/services.php";

    /**
     * Get config service for use in inline setup below
     */
    $config = $di->getConfig();

    /**
     * Include Autoloader
     */
    include APP_PATH . '/config/loader.php';

    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);

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

} catch (\Exception $e) {
    echo $e->getMessage() . '<br>';
    echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

I'd be happy to help, but you should post your own thread rather than reviving a 3 year old thread.



31.2k

and thread is marked as "solved" !

I'd be happy to help, but you should post your own thread rather than reviving a 3 year old thread.

Thank you for the help! I will try the developer tools.

@Dylan - Good point! I was just frustrated and this thread just added to the many questions I had about setting up Volt!