{"id":1501,"date":"2021-01-09T06:04:03","date_gmt":"2021-01-09T06:04:03","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/check-third-party-library-existence-magento-2\/"},"modified":"2025-05-22T11:15:19","modified_gmt":"2025-05-22T05:45:19","slug":"check-third-party-library-existence-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/check-third-party-library-existence-magento-2\/","title":{"rendered":"How to Check If Third Party Library Exists or not in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 works with numerous third-party libraries and also accommodates for external libraries undoubtedly!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are looking forward to extend default Magento functionality with the third party APIs, you require to include external libraries in your extension or feature.&nbsp;<a href=\"https:\/\/developer.adobe.com\/commerce\/php\/architecture\/\" target=\"_blank\" rel=\"noreferrer noopener\">Third-party libraries<\/a>&nbsp;are a great way to add complex features or functionality to your&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-extensions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 extensions<\/a>&nbsp;quickly. Payment gateways, shipping API, social networking, all provide their language-specific library, which we need to include in our Magento extension to connect with them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s take an example where you require the users to install a third party library using composer. What if the users forget to install the library and start making use of the extension? Obviously, the extension will stop working at some point or may generate an error due to non-existence of the third party library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid the situation, I\u2019ve come up with the solution to&nbsp;<strong>check if third party library exists or not in Magento 2.&nbsp;<\/strong>Using the steps, you can create an event to check existence of the third party library and set restriction error to further working of the functionality until the library is installed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to&nbsp;<strong>Check If Third Party Library Exists or Not in Magento 2<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, decide the location where you need to check the availability of a third-party library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The core part of checking availability is the&nbsp;<strong>class_exists&nbsp;<\/strong>function that can be used as below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (!class_exists('library name\\Rest\\Client')) {\n$this->messageManager->addErrorMessage('Please install Library to use SMS notification');\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s comprehend this post with one live example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You want to check the existence of the 3rd party library while someone saves the extension configuration. Here, events and observers take place to set up the restriction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In&nbsp;<strong>admin_system_config_changed_section_sms&nbsp;<\/strong>event, pass an observer which is going to check the availability of third party library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the code to pass the event. For more event lists and how they are used, you can refer&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-events\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2&nbsp;Events<\/a>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;event name=\"admin_system_config_changed_section_sms\">\n    &lt;observer name=\"custom_admin_system_config_changed_section_general\"\n              instance=\"Vendor\\Extension\\Observer\\ConfigTwilioObserver\"\/>\n&lt;\/event><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Pass the observer code like below in&nbsp;<strong><strong>Vendor\\Extension\\Observer<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Observer;\n\nuse Magento\\Framework\\Event\\Observer as EventObserver;\nuse Magento\\Framework\\Event\\ObserverInterface;\nuse Magento\\Framework\\Message\\ManagerInterface;\n\n\nclass ConfigTwilioObserver implements ObserverInterface\n{\n    protected $messageManager;\n    protected $helper;\n    \n    public function __construct(\n        ManagerInterface $messageManager\n    )\n    {\n         $this->messageManager = $messageManager;\n    }\n    \n    public function execute(EventObserver $observer)\n    {\n        if (!class_exists('Library Name\\Rest\\Client')) {\n            $this->messageManager->addErrorMessage('Please install Library to use SMS notification');\n            return false;\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Setting a simple validation can save you a lot of support queries, right?!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the above solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 works with numerous third-party libraries and also accommodates for external libraries undoubtedly! If you are looking forward to extend default Magento functionality with&#8230;<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1501","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1501"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1501\/revisions"}],"predecessor-version":[{"id":14562,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1501\/revisions\/14562"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}