2014-04-30 160 views
0

composer.json:Smarty_Internal_TemplateCompilerBase沒有找到

{ 
    "require": { 
    "smarty/smarty": "v3.1.17" 
    } 
} 

的index.php:

define('SMARTY_SPL_AUTOLOAD', 1); // now smarty should use its own autoloader 

require_once __DIR__ . "/vendor/autoload.php"; 

function my_classes_loader($class) { 
    $path = "{$class}.class.php"; 

    if (file_exists($path)) { 
    include_once $path; 
    return true; 
    } 

    return false; 
} 

spl_autoload_register('my_classes_loader'); 

$smarty = new Smarty(); 
$smarty->setCompileDir("templates_c"); 

$smarty->display('main.html'); 

exit(); 

如果我在瀏覽器中打開它,我得到

Fatal error: Class 'Smarty_Internal_TemplateCompilerBase' not found in //smarty-example/vendor/smarty/smarty/distribution/libs/sysplugins/smarty_internal_smartytemplatecompiler.php on line XX

的文件是存在的。它有內容。並且它對於PHP等是可訪問/可讀的。

我在做什麼錯?什麼不見​​了?

回答

0

這是一個不錯的主意,只有一點決定自動加載,這應該是作曲家一個人。

嘗試將自己的自動加載功能移走,而不是在composer.json中使用自動加載聲明。不幸的是,你沒有使用PSR-0或PSR-4命名標準,但是Composer允許你在這種情況下使用「classmap」。考慮將所有文件名移動以符合PSR-0。

聰明的自動加載應該已經通過需要通過Composer來完成。無需設置該常數。

最後但並非最不重要我認爲你的自動加載函數不應該返回任何東西。尤其是它不應該返回假,如果它找不到它包含該類的文件,因爲根據自動加載函數在堆棧中的排序方式,可能會首先爲所有類調用函數,包括所有的Smarty類。如果在這些情況下返回false,則不允許稍後的函數加載該類,從而銷燬工作的自動載入堆棧。

所以最重要的是最好使用Composer進行所有自動加載。開發人員盡一切努力提供性能最佳的自動加載功能 - 您自己的功能可能只能與他們的功能一樣快,但可能會變慢。