0

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.

4
  • What version of 2.0 are you running (1.0.0-beta)? You should take a look at the latest 2.0.x version because there are some changes to composer.json. Also take a look at alankent.me/2015/10/13/… Feb 23, 2016 at 17:29
  • I am using the beta version. Let me download the latest version and try it again. Thanks.
    – Srinivas
    Feb 23, 2016 at 21:18
  • I downloaded 2.0.2, installed it, did all the upgrades, made sure the site is working as expected. Added my module and made sure the controller class is getting called. I tried psr-4 approach without any luck. Maybe my approach to this problem is wrong. Let me state what I am trying to do. I want somebody to comment on the correct approach. I have a module that has all the stuff (blocks, controller, models etc), in Magento 1 I had the library files (bunch of files that makes a curl request to a server and parse the JSON response into php objects) in lib directory. Based on my
    – Srinivas
    Feb 25, 2016 at 20:55
  • understanding I tried to add the lib files in vendor. that did not work. After further reading I placed the files in <magento install dir>/lib. The structure is lib/easyask/easyask/lib/EasyAsk/Impl. I have composer.json file in lib/easyask/easyask directory that has "autoload": { "psr-4": { "EasyAsk\\": "lib/" } }. Is this the right approach? BTW, this did not work either. When i did setup:di:compile, I get Class does not exist error for EasyAsk\Imple\RemoteFactory. Should I just place the files in app/code/EasyAsk/lib directory?
    – Srinivas
    Feb 25, 2016 at 21:02

2 Answers 2

0

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

11
  • Thank you @david-verholen for the response. I made the change in the namespace declaration, but without any luck. I still don't see the entry in vendor/composer/autoload_namespaces.php.
    – Srinivas
    Feb 23, 2016 at 21:09
  • I think the trailing backslash is required when using PSR4, not with PSR0: stackoverflow.com/a/21463602/616307. Feb 23, 2016 at 22:58
  • @Srinivas After making changes to your modules, make sure to update things. You can't just change it inside the vendor folder and expect it to work; therefore you need the composer dump-autoload command to recompile things as mentioned above. I would suggest to go with PSR4 anyway, in my opinion its easier to follow the paths when using it, and easier to make use of it in composer.json. Feb 23, 2016 at 23:01
  • @stefandoorn Thanks for your suggestion, I changed it to psr4. Yes whenever I make any changes I have been doing composer dump-autoload. I feel I am making a very simple mistake in the code and it still is not working. Here are the changes I made:1. Changed the vendor directory to easyask/easyask. 2. composer.json in vendor/easyask/easyask has "autoload": { "psr-4": { "EasyAsk\\": "lib/EasyAsk/" } } 3. I changed the reference in module composer.json to point to easyask/easyask. I don't see easyask reference in vendor/composer/autoload_psr4.php.
    – Srinivas
    Feb 24, 2016 at 18:22
  • using psr-4 is most likely the better and easier approach. Did you change your directory Structure in your lib as well? with psr-4 defined like in your psr-0 Example, the Classfile would have to be in the directory lib/Impl/RemoteFactory.php Feb 24, 2016 at 18:22
0

Take a look how Magento 2 itself did this in Magento_Base modules' composer.json, inside extra key. I was able to utilise the same syntax for a php library in one of my modules. For reference, I just pasted that chunk of code below:

"extra": {
    "component_paths": {
        "trentrichardson/jquery-timepicker-addon": "lib/web/jquery/jquery-ui-timepicker-addon.js",
        "components/jquery": [
            "lib/web/jquery.js",
            "lib/web/jquery/jquery.min.js",
            "lib/web/jquery/jquery-migrate.js"
        ],
        "blueimp/jquery-file-upload": "lib/web/jquery/fileUploader",
        "components/jqueryui": "lib/web/jquery/jquery-ui.js",
        "twbs/bootstrap": "lib/web/jquery/jquery.tabs.js",
        "tinymce/tinymce": "lib/web/tiny_mce"
    },
    "map": [
        [
            ".htaccess",
            ".htaccess"
        ],
        [
            ".htaccess.sample",
            ".htaccess.sample"
        ],
        [
            ".php_cs",
            ".php_cs"
        ],
        [
            ".travis.yml",
            ".travis.yml"
        ],
        [
            ".user.ini",
            ".user.ini"
        ],
        [
            "CHANGELOG.md",
            "CHANGELOG.md"
        ],
        [
            "CONTRIBUTING.md",
            "CONTRIBUTING.md"
        ],
        [
            "COPYING.txt",
            "COPYING.txt"
        ],
        [
            "Gruntfile.js.sample",
            "Gruntfile.js.sample"
        ],
        [
            "ISSUE_TEMPLATE.md",
            "ISSUE_TEMPLATE.md"
        ],
        [
            "LICENSE.txt",
            "LICENSE.txt"
        ],
        [
            "LICENSE_AFL.txt",
            "LICENSE_AFL.txt"
        ],
        [
            "app/.htaccess",
            "app/.htaccess"
        ],
        [
            "app/autoload.php",
            "app/autoload.php"
        ],
        [
            "app/bootstrap.php",
            "app/bootstrap.php"
        ],
        [
            "app/design/adminhtml/Magento",
            "app/design/adminhtml/Magento"
        ],
        [
            "app/design/frontend/Magento",
            "app/design/frontend/Magento"
        ],
        [
            "app/etc/NonComposerComponentRegistration.php",
            "app/etc/NonComposerComponentRegistration.php"
        ],
        [
            "app/etc/di.xml",
            "app/etc/di.xml"
        ],
        [
            "app/functions.php",
            "app/functions.php"
        ],
        [
            "bin/.htaccess",
            "bin/.htaccess"
        ],
        [
            "bin/magento",
            "bin/magento"
        ],
        [
            "dev/.htaccess",
            "dev/.htaccess"
        ],
        [
            "dev/tests/api-functional/.gitignore",
            "dev/tests/api-functional/.gitignore"
        ],
        [
            "dev/tests/api-functional/_files",
            "dev/tests/api-functional/_files"
        ],
        [
            "dev/tests/api-functional/config",
            "dev/tests/api-functional/config"
        ],
        [
            "dev/tests/api-functional/framework",
            "dev/tests/api-functional/framework"
        ],
        [
            "dev/tests/api-functional/phpunit.xml.dist",
            "dev/tests/api-functional/phpunit.xml.dist"
        ],
        [
            "dev/tests/api-functional/testsuite/Magento",
            "dev/tests/api-functional/testsuite/Magento"
        ],
        [
            "dev/tests/functional/.gitignore",
            "dev/tests/functional/.gitignore"
        ],
        [
            "dev/tests/functional/.htaccess.sample",
            "dev/tests/functional/.htaccess.sample"
        ],
        [
            "dev/tests/functional/bootstrap.php",
            "dev/tests/functional/bootstrap.php"
        ],
        [
            "dev/tests/functional/composer.json",
            "dev/tests/functional/composer.json"
        ],
        [
            "dev/tests/functional/credentials.xml.dist",
            "dev/tests/functional/credentials.xml.dist"
        ],
        [
            "dev/tests/functional/etc",
            "dev/tests/functional/etc"
        ],
        [
            "dev/tests/functional/isolation.php",
            "dev/tests/functional/isolation.php"
        ],
        [
            "dev/tests/functional/lib",
            "dev/tests/functional/lib"
        ],
        [
            "dev/tests/functional/phpunit.xml.dist",
            "dev/tests/functional/phpunit.xml.dist"
        ],
        [
            "dev/tests/functional/tests",
            "dev/tests/functional/tests"
        ],
        [
            "dev/tests/functional/testsuites/Magento",
            "dev/tests/functional/testsuites/Magento"
        ],
        [
            "dev/tests/functional/utils",
            "dev/tests/functional/utils"
        ],
        [
            "dev/tests/integration/.gitignore",
            "dev/tests/integration/.gitignore"
        ],
        [
            "dev/tests/integration/etc",
            "dev/tests/integration/etc"
        ],
        [
            "dev/tests/integration/framework",
            "dev/tests/integration/framework"
        ],
        [
            "dev/tests/integration/phpunit.xml.dist",
            "dev/tests/integration/phpunit.xml.dist"
        ],
        [
            "dev/tests/integration/testsuite/Magento",
            "dev/tests/integration/testsuite/Magento"
        ],
        [
            "dev/tests/integration/tmp",
            "dev/tests/integration/tmp"
        ],
        [
            "dev/tests/js/JsTestDriver/.gitignore",
            "dev/tests/js/JsTestDriver/.gitignore"
        ],
        [
            "dev/tests/js/JsTestDriver/framework",
            "dev/tests/js/JsTestDriver/framework"
        ],
        [
            "dev/tests/js/JsTestDriver/jsTestDriver.php.dist",
            "dev/tests/js/JsTestDriver/jsTestDriver.php.dist"
        ],
        [
            "dev/tests/js/JsTestDriver/jsTestDriverOrder.php",
            "dev/tests/js/JsTestDriver/jsTestDriverOrder.php"
        ],
        [
            "dev/tests/js/JsTestDriver/run_js_tests.php",
            "dev/tests/js/JsTestDriver/run_js_tests.php"
        ],
        [
            "dev/tests/js/JsTestDriver/testsuite/lib",
            "dev/tests/js/JsTestDriver/testsuite/lib"
        ],
        [
            "dev/tests/js/JsTestDriver/testsuite/mage",
            "dev/tests/js/JsTestDriver/testsuite/mage"
        ],
        [
            "dev/tests/js/jasmine",
            "dev/tests/js/jasmine"
        ],
        [
            "dev/tests/static/.gitignore",
            "dev/tests/static/.gitignore"
        ],
        [
            "dev/tests/static/framework",
            "dev/tests/static/framework"
        ],
        [
            "dev/tests/static/get_github_changes.php",
            "dev/tests/static/get_github_changes.php"
        ],
        [
            "dev/tests/static/phpunit-all.xml.dist",
            "dev/tests/static/phpunit-all.xml.dist"
        ],
        [
            "dev/tests/static/phpunit.xml.dist",
            "dev/tests/static/phpunit.xml.dist"
        ],
        [
            "dev/tests/static/testsuite/Magento",
            "dev/tests/static/testsuite/Magento"
        ],
        [
            "dev/tests/unit/.gitignore",
            "dev/tests/unit/.gitignore"
        ],
        [
            "dev/tests/unit/framework",
            "dev/tests/unit/framework"
        ],
        [
            "dev/tests/unit/phpunit.xml.dist",
            "dev/tests/unit/phpunit.xml.dist"
        ],
        [
            "dev/tests/unit/tmp",
            "dev/tests/unit/tmp"
        ],
        [
            "dev/tools",
            "dev/tools"
        ],
        [
            "dev/travis",
            "dev/travis"
        ],
        [
            "index.php",
            "index.php"
        ],
        [
            "lib/.htaccess",
            "lib/.htaccess"
        ],
        [
            "lib/internal/LinLibertineFont",
            "lib/internal/LinLibertineFont"
        ],
        [
            "lib/web/FormData.js",
            "lib/web/FormData.js"
        ],
        [
            "lib/web/MutationObserver.js",
            "lib/web/MutationObserver.js"
        ],
        [
            "lib/web/blank.html",
            "lib/web/blank.html"
        ],
        [
            "lib/web/css",
            "lib/web/css"
        ],
        [
            "lib/web/es6-collections.js",
            "lib/web/es6-collections.js"
        ],
        [
            "lib/web/extjs",
            "lib/web/extjs"
        ],
        [
            "lib/web/fonts",
            "lib/web/fonts"
        ],
        [
            "lib/web/fotorama",
            "lib/web/fotorama"
        ],
        [
            "lib/web/i18n",
            "lib/web/i18n"
        ],
        [
            "lib/web/images",
            "lib/web/images"
        ],
        [
            "lib/web/jquery.js",
            "lib/web/jquery.js"
        ],
        [
            "lib/web/jquery",
            "lib/web/jquery"
        ],
        [
            "lib/web/knockoutjs",
            "lib/web/knockoutjs"
        ],
        [
            "lib/web/legacy-build.min.js",
            "lib/web/legacy-build.min.js"
        ],
        [
            "lib/web/less",
            "lib/web/less"
        ],
        [
            "lib/web/lib",
            "lib/web/lib"
        ],
        [
            "lib/web/mage",
            "lib/web/mage"
        ],
        [
            "lib/web/magnifier",
            "lib/web/magnifier"
        ],
        [
            "lib/web/matchMedia.js",
            "lib/web/matchMedia.js"
        ],
        [
            "lib/web/modernizr",
            "lib/web/modernizr"
        ],
        [
            "lib/web/moment-timezone-with-data.js",
            "lib/web/moment-timezone-with-data.js"
        ],
        [
            "lib/web/moment.js",
            "lib/web/moment.js"
        ],
        [
            "lib/web/prototype",
            "lib/web/prototype"
        ],
        [
            "lib/web/requirejs",
            "lib/web/requirejs"
        ],
        [
            "lib/web/scriptaculous",
            "lib/web/scriptaculous"
        ],
        [
            "lib/web/spacer.gif",
            "lib/web/spacer.gif"
        ],
        [
            "lib/web/tiny_mce",
            "lib/web/tiny_mce"
        ],
        [
            "lib/web/underscore.js",
            "lib/web/underscore.js"
        ],
        [
            "lib/web/varien",
            "lib/web/varien"
        ],
        [
            "nginx.conf.sample",
            "nginx.conf.sample"
        ],
        [
            "package.json.sample",
            "package.json.sample"
        ],
        [
            "php.ini.sample",
            "php.ini.sample"
        ],
        [
            "phpserver",
            "phpserver"
        ],
        [
            "pub/.htaccess",
            "pub/.htaccess"
        ],
        [
            "pub/.user.ini",
            "pub/.user.ini"
        ],
        [
            "pub/cron.php",
            "pub/cron.php"
        ],
        [
            "pub/errors",
            "pub/errors"
        ],
        [
            "pub/get.php",
            "pub/get.php"
        ],
        [
            "pub/index.php",
            "pub/index.php"
        ],
        [
            "pub/media/.htaccess",
            "pub/media/.htaccess"
        ],
        [
            "pub/media/customer/.htaccess",
            "pub/media/customer/.htaccess"
        ],
        [
            "pub/media/downloadable/.htaccess",
            "pub/media/downloadable/.htaccess"
        ],
        [
            "pub/media/import",
            "pub/media/import"
        ],
        [
            "pub/media/theme_customization/.htaccess",
            "pub/media/theme_customization/.htaccess"
        ],
        [
            "pub/opt",
            "pub/opt"
        ],
        [
            "pub/static.php",
            "pub/static.php"
        ],
        [
            "pub/static/.htaccess",
            "pub/static/.htaccess"
        ],
        [
            "setup",
            "setup"
        ],
        [
            "var/.htaccess",
            "var/.htaccess"
        ],
        [
            "vendor/.htaccess",
            "vendor/.htaccess"
        ]
    ]
}

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.