We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Loading SwiftMailer without Composer autoloader

Hello, guys. I am new in PHP and Phalcon, so i can't understand many things. Please, tell me how i can properly load SwiftMailer into my app without Composer autoloader. I use next approach:

loader.php:

require_once($config->application->vendorsDir.'swiftmailer/swiftmailer/lib/swift_required.php');

$loader = new \Phalcon\Loader();

$loader->registerPrefixes(
    array(
       "Swift_"    => $config->application->vendorsDir.'swiftmailer/swiftmailer/lib/classes/Swift',
    )
);

But is this right? Should i use this require_once in that place? If not, please tell, where it must be?



642
Accepted
answer

I am sorry, but why don't you use the Composer?

composer.json:

{
    "require": {
        "swiftmailer/swiftmailer": "v5.4.0",
    }
}

loader.php:

    /**
     * Include composer autoloader
     */
    require __DIR__ . $config->application->vendorsDir . '/autoload.php';

somewhere in the project:

$message = \Swift_Message::newInstance()


9.7k

I thought, using Composer autoloader with Phalcon autoloader will slow my app. Today tested it with WebGrind and seems like it's not. The speed is almost the same. Thank you for answer!



642
edited Apr '15

Classmap might help you, if you have a lot of packages and worry about performace.

https://getcomposer.org/doc/04-schema.md#classmap

Additionally, it can dump an optimized autoloader that converts PSR-0/4 packages into classmap ones for performance reasons. In large applications with many classes, the autoloader can take up a substantial portion of every request's time. Using classmaps for everything is less convenient in development, but using this option you can still use PSR-0/4 for convenience and classmaps for performance.

https://getcomposer.org/doc/03-cli.md#dump-autoload

edited Apr '15

I don't autoload SwiftMailer - I just add it to my DI. No need to mess with Composer or autoloading because the path and library isn't going to be changing.



9.7k
edited Apr '15

I don't autoload SwiftMailer - I just add it to my DI. No need to mess with Composer or autoloading because the path and library isn't going to be changing.

In my case phalcon-mailer library require this dependencies. So, i don't know how to resolve dependencies, like

use Swift_Mailer;
use Swift_Message;

without Comoser autoloader.

Just download the SwiftMailer package from GitHub: https://github.com/swiftmailer/swiftmailer.

I should note that if you've got it working with Composer, there's not really much point in doing things this way. I was just posting for posterity in case someone else had the same question as you in the future, and wanted another option other than Composer.



187

I am sorry, but why don't you use the Composer?

composer.json:

{
   "require": {
       "swiftmailer/swiftmailer": "v5.4.0",
   }
}

loader.php:

   /**
    * Include composer autoloader
    */
   require __DIR__ . $config->application->vendorsDir . '/autoload.php';

somewhere in the project:

$message = \Swift_Message::newInstance()


187

thank you for you answer