2012-10-31 177 views
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,但提示錯誤信息爾康伏模板引擎

Notice: Undefined variable: view in /var/www/html/phalconblog/public/index.php on line 40

Notice: Undefined variable: di in /var/www/html/phalconblog/public/index.php on line 40

Fatal error: Call to undefined method Phalcon\Mvc\View\Engine\Volt::setOptions() in /var/www/html/phalconblog/public/index.php on line 42

回答

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