AutoLoading External Libraries

So i've been trying to auto load any external libraries ( like Mustache , PHPExcel ... ) , and all of them come's with an autoloader.php , but i tried to use the registerDirs , but i can't make it work , but if i load all of the classes manualy it works fine. Why so ?

(new \Mustache_Engine)->render("/contact.tpl", [ 
    'name' => $this->request->getPost("name") , 
    'message' => $this->request->getPost("message")
]);

This doesn't work:

$loader->registerDirs([
    '../libraries/Mustache/'
]);

This works:

$loader->registerClasses([
            'Mustache_Engine'           => '../libraries/Mustache/Engine.php',
            'Mustache_Cache'            => '../libraries/Mustache/Cache.php',
            'Mustache_Compiler'         => '../libraries/Mustache/Compiler.php',
            'Mustache_Context'          => '../libraries/Mustache/Context.php',
            'Mustache_Exception'        => '../libraries/Mustache/Exception.php',
            'Mustache_HelperCollection' => '../libraries/Mustache/HelperCollection.php',
            'Mustache_LambadaHelper'    => '../libraries/Mustache/LambadaHelper.php',
            'Mustache_Loader'           => '../libraries/Mustache/Loader.php',
            'Mustache_Logger'           => '../libraries/Mustache/Logger.php',
            'Mustache_Parser'           => '../libraries/Mustache/Parser.php',
            'Mustache_Template'         => '../libraries/Mustache/Template.php',
            'Mustache_Tokenizer'        => '../libraries/Mustache/Tokenizer.php',

            'Mustache_Cache_AbstractCache'     => '../libraries/Mustache/Cache/AbstractCache.php',
            'Mustache_Cache_FilesystemCache'   => '../libraries/Mustache/Cache/FilesystemCache.php',
            'Mustache_Cache_NoopCache'         => '../libraries/Mustache/Cache/NoopCache.php',

            'Mustache_Loader_StringLoader'     => '../libraries/Mustache/Loader/StringLoader.php',
            'Mustache_Loader_MutableLoader'    => '../libraries/Mustache/Loader/MutableLoader.php',
            'Mustache_Loader_InlineLoader'     => '../libraries/Mustache/Loader/InlineLoader.php',
            'Mustache_Loader_FilesystemLoader' => '../libraries/Mustache/Loader/FilesystemLoader.php',
            'Mustache_Loader_CascadingLoader'  => '../libraries/Mustache/Loader/CascadingLoade.php',
            'Mustache_Loader_ArrayLoader'      => '../libraries/Mustache/Loader/ArrayLoader.php',

            'Mustache_Logger_AbstractLogger'   => '../libraries/Mustache/Logger/AbstractLogger.php',
            'Mustache_Logger_StreamLogger'     => '../libraries/Mustache/Logger/StreamLogger.php',

            'Mustache_Exception_InvalidArgumentException' => '../libraries/Mustache/Exception/InvalidArgumentException.php',
            'Mustache_Exception_LogicException'           => '../libraries/Mustache/Exception/LogicException.php',
            'Mustache_Exception_RuntimeException'         => '../libraries/Mustache/Exception/RuntimeException.php',
            'Mustache_Exception_SyntaxException'          => '../libraries/Mustache/Exception/SyntaxException.php',
            'Mustache_Exception_UnknownFilterException'   => '../libraries/Mustache/Exception/UnknownFilterException.php',
            'Mustache_Exception_UnknownHelperException'   => '../libraries/Mustache/Exception/UnknownHelperException.php',
            'Mustache_Exception_UnknownTemplateException' => '../libraries/Mustache/Exception/UnknownTemplateException.php',
        ]);
edited Apr '15

Are you installing the libraries with composer? (if you are not, tell us how do you install them)

Ir you are doing so, you just need to include the autoload.php file that will appear in the vendors folder. I do it in the index php file. Just place this on the top part of your index.php file.

require_once '../vendor/autoload.php';

(adapt the path to suit yours)

Well i just grab the source files and put in a libraries directory , like so :

| apps
| config
| libraries
    | PHPMailer
    | PHPExcel
    | Mustache
| public


5.9k

you better use composer to manage your dependency