Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Adding external libs to the project

Hi All,

Having the issue,

Cannot add the Phalcon mailer and use it in the project,

Doing same that instructions says .

I have config linking to 'libraryDir' => APP_PATH . '/app/lib/' in the lib folder I have a Mailer folder where I put the composer.json , folowed https://github.com/phalcon-ext/mailer

But after using the lib in the Controller I've got

Fatal error</b>: Uncaught Error: Class 'Phalcon\Ext\Mailer\Manager'

Code :

use Phalcon\Http\Response;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Mvc\Model\Query\BuilderInterface;
use Phalcon\Di\InjectionAwareInterface;
use Phalcon\Mvc\Model\Resultset;
use Phalcon\Ext\Mailer\Manager;
use Phalcon\Ext\Mailer\Message;

class EmailController extends Phalcon\Mvc\Controller
{
    public function initialize() {
        if ($this->request->isPost()) {
            $this->view->disable();
        }
    }

    public function indexAction() {

    }

    public function sendEmailAction(){
        if ($this->request->isPost()) {

            $config = [
                'driver'     => 'mail',
                'from'       => [
                    'email' => '[email protected]',
                    'name'  => 'Email'
                ]
            ];

            $email = new Phalcon\Ext\Mailer\Message($config);
            return "send";
        }
    }

}
edited Dec '17

You have to follow Phalcon\Ext\Mailer installation instructions. There is no instructions about putting somewhere composer.json. Just install this library using typical for the Composer way:

composer require "phalcon-ext/mailer":"~2.0"

The last thing you need: make sure that you are using Composer autoloader:

require_once 'vendor/autoload.php';
edited Dec '17

So , in fact I did the same , it was successfully installed , but what I get after was

Fatal error</b>: require_once(): Failed opening required '../../../vendor/autoload.php' (include_path='.:/Applications/MAMP/bin/php/php7.1.8/lib/php')

The structure od my project is

  • Root
  • -fromtend
  • -backend(phlacon)
  • --app
  • ---controllers (here I'd like to use the lib)
  • --public
  • --vendor
  • ---autoload.php

Also let me add that I'm using another libs , and the require() works good as well as usage of them ....

You have to follow Phalcon\Ext\Mailer installation instructions. There is no instructions about putting somewhere composer.json. Just install this library using typical for the Composer way:

composer require "phalcon-ext/mailer":"~2.0"

The last thing you need: make sure that you are using Composer autoloader:

require_once 'vendor/autoload.php';

Could you please provide public/index.php

edited Dec '17
<?php
header('Access-Control-Allow-Origin: *');

use Phalcon\Http\Response;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Mvc\Model\Query\BuilderInterface;
use Phalcon\Di\InjectionAwareInterface;
use Phalcon\Mvc\Model\Resultset;

error_reporting(E_ALL);

define('APP_PATH', realpath('..'));

try {

/**
* Read the configuration
*/
$config = include APP_PATH . "/app/config/config.php";

/**
* Read auto-loader
*/
include APP_PATH . "/app/config/loader.php";

/**
* Read services
*/
include APP_PATH . "/app/config/services.php";

include APP_PATH . "/app/constants/constants.php";

include APP_PATH . "/app/lib/phpthumb/ThumbLib.inc.php";

include APP_PATH . "/app/lib/simple_html_dom_parser_1_5/simple_html_dom.php";

foreach (glob(APP_PATH . "/app/lib/*.php") as $filename)
{
include $filename;
}

include APP_PATH."/app/test/BaseTest.php";
foreach (glob(APP_PATH . "/app/test/*.php") as $filename)
{
if (strpos($filename, "BaseTest") === false) {
include $filename;
}
}

/**
* Handle the request
*/
$application = new \Phalcon\Mvc\Application($di);

echo $application->handle()->getContent();

} catch (\Exception $e) {
echo $e->getMessage() . '<br>';
echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

Could you please provide public/index.php

edited Dec '17

You have update public/index.php by adding require_once '../vendor/autoload.php';:

   /**
    * Read auto-loader
    */
   include APP_PATH . "/app/config/loader.php";
   require_once '../vendor/autoload.php';


662
Accepted
answer
edited Dec '17

Update thats fixed , needed to add include '../../vendor/autoload.php'; 2 directoties back , now dealing with

Service '\Swift_MailTransport' wasn't found in the dependency injection container<br><pre>#0


You have update public/index.php by adding require_once '../vendor/autoload.php';:

  /**
   * Read auto-loader
   */
  include APP_PATH . "/app/config/loader.php";
  require_once '../vendor/autoload.php';

This means you don't have Swift Mailer installed.

Use

composer require "swiftmailer/swiftmailer":"^5.4"

to install Swift Mailer

IDK how people end up with this - entire purpose of using autoloading and composer is to make it as simple as possible to load all your project dependncies at one place.

It maybe obvios for you , I use it for the first time , also , there was not obvios for me isue , now tnx to the Serghei , I know how to do it . I thought that forum is for helping Beginners also , aren't it ? Or when you start developing everything was clear to you and you have no trivial question ?

IDK how people end up with this - entire purpose of using autoloading and composer is to make it as simple as possible to load all your project dependncies at one place.

Sorry - I didn't mean to put you down! Not at all. I just seen too many times problems of this kind thus made that comment (which sounded too harsh). Of course that's what we have forums for - to ask questions and get answers!