如何在Phalcon中设置伏特编译的目录路径?

时间:2012-10-31 10:12:23

标签: php phalcon

我有以下代码:

    $di->set('view', function() {

    $view = new \Phalcon\Mvc\View();

    $view->setViewsDir('../app/views/');

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

    return $view;
});

但是现在已编译的PHP存在于views目录中。如何为编译目录设置不同的路径?

1 个答案:

答案 0 :(得分:5)

您可以设置新的compiledPath和其他选项,如下所示:

假设您的配置中包含以下变量:

[views]
path      = '/home/user/www/app/views/'

[volt]
path      = '/home/user/www/app/volt/'
extension = '.compiled'
separator = '%%'
stat      = 1

然后您可以根据手册执行此操作:

// 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;
    }
);

或者您可以按照以下实施方式(首选上述方法)

$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;
});

确保您的$config->volt->path可写。您无需完全遵循上述方法 - 您始终可以将配置变量替换为您的应用程序所需的任何内容。