Braindumps on a Magento bridge in Joomla!
Saturday, 14 March 2009As Joomla! is the prime CMS nowadays and Magento the prime e-commerce solution, people wonder if it is possible to build a bridge between Joomla! and Magento. There are various approaches that can be used to build such a bridge, but some are unwanted and others impossible. In the article I will lay them out, as I've been working on such a bridge myself.
Three options
There are mainly three options to integrate Magento into Joomla!. Note that Mark Kimsals book on Magento programming already guides us into implementing a full integration. Also note that this blog about integrating Magento into Joomla!, not integrating Joomla! into Magento. The goal is to use Magento as a Joomla! component, not to insert Joomla! articles as CMS Pages into Magento.
The three options we have for integration are:
- Database-level integration
- Integration using the SOAP or XML-RPC API
- Integration using the Magento PHP-classes
Database-level integration
Database-level integration seems the easiest as long as you know the database structure of Magento. The Joomla! JDatabase class can facilitate in connecting a Joomla! bridge to the Magento database. This allows you to dive into the stored data of Magento.
One down side here is that you will bump inevitably into the EAV-architecture (Entity Attribute Value) used by Magento database, which complicates fetching data from the database. Second, none of the PHP-logic of Magento is used. So, this option is nice to fetch some data from the Magento database, but if you want to use a Magento cart inside Joomla!, this is not the way to go.
A third downside is that the Joomla! website needs to access the Magento database. If they are on the same server this does not pose many problems, but it is less secure and should be considered unusable if Joomla! and Magento reside on different servers - never use a database connection over the Internet!
Using SOAP or XML-RPC
The downside of connecting Joomla! to Magento remotely, is solved by using SOAP or XML-RPC. Both SOAP and XML-RPC can be seen as protocols that allows Joomla! to communicate with Magento through HTTP. Instead of just transferring just HTML through HTTP, XML is used here, which allows for more complex data communication.
Joomla! does not ship with SOAP but does ship with a XML-RPC client (the "xmlrpc_client" class inside /libraries/phpxmlrpc). Besides that, many PHP5-installations have XML-RPC and SOAP extensions built-in. Within Joomla! a client could be constructed that calls the Magento API for something, for instance returning a list of products.
Within Magento a proxy-user has to be created and this proxy-user has to be given permissions. This allows for further security, because you can allow the proxy-user (as used by the XML-RPC-client in Joomla!) to view and modify products, but not handle customers at all.
The upside here is that data exchange is handled in a more professional way, but still - if you want to use a Magento shopping cart inside Joomla!, you still need to build everything yourself. In those cases a full integration is needed.
Integrating Magento classes in Joomla!
Any serious effort to integrate Magento into Joomla! should head this way. By calling Magento-classes from within the Joomla! CMS you can reuse every piece of Magento functionality in Joomla!, ranging from calling helpers to including complete Magento blocks (and the shopping cart could be such a block).
The php|architect's book "Guido to E-commerce Programming with Magento" (author Mark Kimsal) gives some examples on how to use Magento-code outside Magento, so inside for instance a Joomla! extension. But when trying out this code, I myself ran into several problems. Besides several constants (like DS) being defined in both Joomla! as Magento (which is illegal) the main problem here is __autoload.
The magic function __autoload allows for calling classes, that have not yet been defined in an application. Take for instance a class called HelloWorld - to use this class the PHP-code for this class needs to be defined somewhere. Normally you would include for example a file called "helloworld.php" (containing the code for HelloWorld) before actually using HelloWorld. The __autoload function allows for including "helloworld.php" automatically as soon as HelloWorld is needed.
Both Joomla! as Magento use the __autoload function, but in PHP a function can only be declared once. Using the Magento framework inside a Joomla! extension will there for throw a PHP Fatal Error. The only solution to this is to replace __autoload with spl_autoload_register(), but this is something Magento or Joomla! has to fix. Joomla! 1.6 has already fixed this problem, but is not yet available to the public. Till then a full integration can not be accomplished.
Using AJAX
The problem is that the Joomla! Framework can not be combined with the Magento Framework. But from within Joomla! you can make a call to a simple PHP-script that has no framework at all, and that script can include the Magento Framework without problems. This is the stuff I'm working on myself, allowing for flexible reusage of Magento blocks in Joomla! extensions. I will keep you all posted.
Licensing
Besides the technical difficulties it should also be noted that the GNU/General Public License (GPL) under which Joomla! is released, requires all extensions to be GPL as well. As Magento is not GPL-ed but released under the Open Source License, this causes a licensing conflict. Some people consider this a show-stopper. I don't, as the GPL is ment for freeing things, not constraining them.
Work in progress
At the moment I have created a development versio of a Magento Bridge for Joomla! based on the ideas above. This bridge offers the option to display Magento blocks inside the Joomla! component-area. It also ships with the Magento XML-RPC client to allow further extension development. A preview might be available soon.
