2012-10-31 37 views
2

我有以下代碼:如何在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目錄。我怎樣才能爲編譯目錄設置不同的路徑?

回答

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是可寫的。您無需完全遵循上述方法 - 您可以隨時隨地將配置變量替換爲您的應用程序所需的任何內容。

+0

我收到:致命錯誤:調用未定義的方法Phalcon \ Mvc \ View \ Engine \ Volt :: setOptions()在/var/www/html/phalconblog/public/index.php上59行# – netstu

+1

請重新編譯擴展名。我在測試上述代碼時遇到了與代碼相同的問題。 –

+0

我重新推薦phalcon 0.6.0,它工作正常 – netstu

相關問題