Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I include an external php file in magento module?

Tags:

php

magento

I am creating some magento module, and I need to use some 3rdParty classes "ThiredPartyClassA" and "ThiredPartyClassB" that are in an external file "thirdPartyCode.php".

where should I place the file thirdPartyCode.php ? and how should I refer (require_once) to if so that I can use it within one of my Action handlers ?

Thanks, Eyal

like image 367
epeleg Avatar asked Dec 01 '22 04:12

epeleg


2 Answers

I ended up creating a lib directory under my modules main directory. It seemed to be the "best of both worlds".

$ExternalLibPath=Mage::getModuleDir('', 'My_Module') . DS . 'lib' . DS .'EXTERNALLIB.php';
require_once ($ExternalLibPath);
like image 146
epeleg Avatar answered Dec 05 '22 20:12

epeleg


You can require files in PHP just as you could without Magento, so you can actually use require_once if need be. If you want to keep your code cleaner, you may want to put it into the /lib folder in Magento since it is a system library. I am not sure if this is in the default include path, so you may have to fiddle with the require.

For cleanliness, you may also want to make a wrapper around this code and use Magento models/helpers to manipulate them.

Hope that helps!

Thanks, Joe

like image 33
Joe Mastey Avatar answered Dec 05 '22 20:12

Joe Mastey