Magento – Magento 2: Cannot load the library using composer

composermagento2

Sorry for the lengthy message. I just wanted to provide as much information as possible. I am writing a new module. I have a custom library that I need to load to use in my module. I have the library classes in vendor/easyask/easyask_search/lib/EasyAsk/Impl. The module is in app/code/EasyAsk/Search.

When I try to invoke EasyAsk\Impl\RemoteFactory, I get a class does not exist error. I know the lib files are not autoloaded as I don't see them referenced in vendor/composer/autoload_namespaces.php file.

Can somebody tell me what I'm doing wrong?

Here is the composer.json in my module

{
"name": "easyask/module-search",
"description": "A better search module from EasyAsk",
"require": {
    "php": "~5.5.0|~5.6.0",
    "magento/module-store": "1.0.0-beta",
    "magento/module-catalog": "1.0.0-beta",
    "magento/module-catalog-search": "1.0.0-beta",
    "magento/module-backend": "1.0.0-beta",
    "magento/module-theme": "1.0.0-beta",
    "magento/framework": "1.0.0-beta",
"easyask/easyask_search": "2.0.0",
    "magento/magento-composer-installer": "*"
},
"type": "magento2-module",
"version": "1.0.0-beta",
"license": [
    "OSL-3.0",
    "AFL-3.0"
],
"extra": {
    "map": [
        [
            "*",
            "EasyAsk/Search"
        ]
    ]
}

}

Here is the composer.json from vendor/easyask/easyask_search directory

{
"name": "easyask/easyask_search",
"type": "magento2-library",
"description": "EasyAsk Search PHP Client Library",
"license": "N/A",
"authors": [
    {
        "name": "EasyAsk",
        "homepage": "http://www.easyask.com"
    }
],
"require": {
    "php": ">=5.2.1",
    "ext-curl": "*",
    "ext-dom": "*",
    "ext-hash": "*",
    "ext-openssl": "*",
    "ext-simplexml": "*",
    "ext-xmlwriter": "*"
},
"require-dev": {
    "phpunit/phpunit": "3.7.*"
},
"autoload": {
    "psr-0": {
        "EasyAsk": "lib"
    }
}

}

Part of controller class where I'm trying to inject RemoteFactory

use Magento\Framework\Event\Manager;
use EasyAsk\Impl\RemoteFactory;

class Index extends \Magento\CatalogSearch\Controller\Result\Index
{
    /**
     * @var QueryFactory
     */
    private $_queryFactory;

    /**
     * Catalog Layer Resolver
     *
     * @var Resolver
     */
    private $layerResolver;

    protected $scopeConfig;

    protected $_eventManager;

    protected $_remoteFactory;

     /**
     * @param Context $context
     * @param Session $catalogSession
     * @param StoreManagerInterface $storeManager
     * @param QueryFactory $queryFactory
     * @param Resolver $layerResolver
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        Context $context,
        Session $catalogSession,
        StoreManagerInterface $storeManager,
        QueryFactory $queryFactory,
        Resolver $layerResolver,
        ScopeConfigInterface $scopeConfig,
        Manager $eventManager,
        RemoteFactory $remoteFactory
    ) {
        parent::__construct($context, $catalogSession, $storeManager, $queryFactory, $layerResolver);
        $this->_storeManager = $storeManager;
        $this->_catalogSession = $catalogSession;
        $this->_queryFactory = $queryFactory;
        $this->layerResolver = $layerResolver;
        $this->scopeConfig = $scopeConfig;
        $this->_eventManager = $eventManager;
        $this->_remoteFactory = $remoteFactory;
    }

Let me know if you need any more information.

Best Answer

if you installed the library via composer, I can only see one potential problem on first sight.

the psr-0 namespace declaration must have (I think) a trailing backslash (which also must be escaped). So it would look like this:

"psr-0": {
    "EasyAsk\\": "lib"
}

The file for the class in your library should then be located in lib/EasyAsk/Impl/RemoteFactory.php and have the namespace EasyAsk\Impl and the class name RemoteFactory

you could also leave out the type magento2-library then which would default to the library and just use the default composer autoloading mechanism.

you can find further information on composer psr-0 autoloading here: https://getcomposer.org/doc/04-schema.md#psr-0

to see if your library is correctly loaded via composer you can also look into the file vendor/composer/autoload_psr0.php (or smth similar). There should be an entry for this.

To recompile the composer autoloading files you can execute the following command in your Magento 2 root dir: composer dumpautoload

Related Topic