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

Help Loading External Libraries

So i'm working in a project and i'm useing Phalcon's Multi so i can have a frontend and backend , but i can't get PHPMailer or any class to work !

I have included a namespace for the libraries as " Frontend\Libraries ", so in the Controller i just include it ( " use Frontend\Libraries\PHPMailer as PHPMailer " ), but when i init the class and var_dump it nothing happens . I also had a classe that i made called Zipper, but that class just displayed the raw code after a " $this->_zip " call inside the class .

$loader->registerNamespaces([
        'Frontend\Controllers'  => __DIR__ . '/controllers/',
        'Frontend\Models'       => __DIR__ . '/models/',
        'Frontend\Libraries'    => __DIR__ . '/library/',
    ]);

Controller

namespace Frontend\Controllers;
use Frontend\Libraries\PHPMailer\phpmailer as PHPMailer;

class RequestController extends ControllerBase
{

    public function SendmailAction()
    {

        $mail = new PHPMailer();
        var_dump($mail);

        # $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
    }

}


7.9k

could you please paste your PHP mailer class?

could you please paste your PHP mailer class?

Well its just the raw file downloaded from https://github.com/PHPMailer/PHPMailer.

could you please paste your PHP mailer class?

This is the other class mentioned :

class zip
{
private $_files = [],
        $_zip;

public function __construct()
{
    $this->_zip = new ZipArchive;
}

public function add($input)
{
    if ( is_array($input) ) :
        $this->_files  = array_merge($this->_files, $input);
    else :
        $this->_files[]  = $input;
    endif;
}

public function save( $location = NULL )
{
    if ( count($this->_files) && $location ) :
        foreach($this->_files as $index => $file) :
            if ( !file_exists($file) ):
                unset($this->_files[$index]);
            endif;
        endforeach;

        if ( $this->_zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE ) ):
            foreach($this->_files as $file) :
                $this->_zip->addFile($file, $file );
            endforeach;
            $this->_zip->close();
        endif;
    endif;
}
}


7.9k

do your zip class have namespace?

do your zip class have namespace?

not in the class file



7.9k
Accepted
answer

Its better add namespace to you zipper class and place in library so you can autoload it

Its better add namespace to you zipper class and place in library so you can autoload it

add " Frontend\Libraries "?



7.9k

depends, are you PSR-4 or autoloading standard?

$loader = new \Phalcon\Loader();

$loader->registerClasses(array( 'Zip' => 'path/to/your/file.php' ));

$loader->register();