1
$di = new \Phalcon\DI\FactoryDefault();

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function() use ($config) {
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri($config->application->baseUri);
    return $url;
});

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

/**
 * Setting up the view component
 */
$di->set('view', function() use ($config) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(".phtml" => 'voltService'));
    return $view;
});

我使用这篇文章:http ://docs.phalconphp.com/en/0.6.0/reference/volt.html ,但提示错误消息

注意:未定义变量:查看 /var/www/html/phalconblog/public/index.php 第 40 行

注意:未定义的变量:第 40 行 /var/www/html/phalconblog/public/index.php 中的 di

致命错误:在第 42 行的 /var/www/html/phalconblog/public/index.php 中调用未定义的方法 Phalcon\Mvc\View\Engine\Volt::setOptions()

4

1 回答 1

3

我也试过了,也有类似的问题——不知道为什么。

编辑

正确的方法是:

// Register Volt as a service
$di->set(
    'volt', 
    function($view, $di) 
    {
        $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,
            )
        );

        return $volt;
    }
);

// Register Volt as template engine
$di->set(
    'view', 
    function() 
    {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir($config->application->viewsDir);

        $view->registerEngines(array(".volt" => 'volt'));

        return $view;
    }
);

以下是一种解决方法,但不建议使用:

/**
 * Setting up the view component
 */
$di->set(
    'view', 
    function() use ($config, $di) 
    {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir($config->application->viewsDir);

        $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('.phtml' => $volt));

        return $view;
    }
);
于 2012-10-31T14:29:06.273 回答