15

We have 4 types of components in Magento 2, as I have been using three of them, but my question is how to use const LIBRARY = 'library'; component.

As name implies, this is something to include third party libraries, but any one can give me an example on how to include a library and use the lib in global application level.

/**#@+
* Different types of components
*/
const MODULE = 'module';
const LIBRARY = 'library';
const THEME = 'theme';
const LANGUAGE = 'language';

I did not find any information about const LIBRARY = 'library'; in the official Magento 2 documentation Register your component.

4 Answers 4

2

I'm not sure why it's not documented on the official documentation but here's how it should be done from what I understand:

So it's very similar to the way your register modules, you just need to create lib/internal/Your/Library/registration.php with the following content:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::LIBRARY,
    'your/library',
    __DIR__
);

Also you can find the right location is properly documented here: http://devdocs.magento.com/guides/v2.0/architecture/archi_perspectives/components/modules/mod_conventions.html

To use/load these libraries you follow the same way as it's done with the Magento\Framework library. So using :

use Your\Library\Custom\Class;

You can find the core registration.php under lib/internal/Magento/Framework folder

3
  • Thanks @Raphael, Can please give me an example on how to use/load these libraries added "LIBRARY = 'library" in third-party/custom implementations. Mar 8, 2017 at 12:35
  • Am looking forward to have basic example on how to use this in real time. Mar 8, 2017 at 12:36
  • @Krishati95Dev see my updated answer Mar 8, 2017 at 12:41
1

You can refer from core file's where it is been used .Here is the example this may help you , You need to give name which you want to include :-

ComponentRegistrar::register(ComponentRegistrar::LIBRARY, '', DIR);

Example :

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::LIBRARY,
    'magento/test',
    __DIR__ );
0

I could get the answer https://magento.stackexchange.com/a/163418/36759 to work, but only after declaring namespaces inside the classes in the library, so keep that in mind.

0

From Magento 2.2 the described half-documented library approach does not work anymore. Magento encourages developers to add libraries using composer according to:

https://github.com/magento/magento2/issues/10985

What works would be adding your own autoloader: lib/internal/Your/Library/registration.php

<?php
namespace Your\Library;

spl_autoload_register(function ($class) {
  $prefix   = __NAMESPACE__ . '\\';
  $base_dir = __DIR__.'/';
  $len = strlen($prefix);

  if (strncmp($prefix, $class, $len) !== 0) {
    return;
  }
  $relative_class = substr($class, $len);
  $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
  if (file_exists($file)) {
    require $file;
  }
});

This works due to app/etc/NonComposerComponentRegistration.php which still includes the following directories:

$pathList[] = dirname(dirname(__DIR__)) . '/lib/internal/*/*/registration.php';
$pathList[] = dirname(dirname(__DIR__)) . '/lib/internal/*/*/*/registration.php';

Your Answer

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

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